基于双值的Java流对象排序

基于双值的Java流对象排序,java,lambda,java-stream,Java,Lambda,Java Stream,我有一个X1对象的列表,并试图根据返回Double值的方法obj_function()对列表进行排序 result = custom_obj_list.stream() .sorted((x1,x2) -> { return -x1.obj_function().compareTo(x2.obj_function()); }).filter((x2) -> { return x2.obj_function <= 1;

我有一个
X1
对象的列表,并试图根据返回
Double
值的方法
obj_function()
对列表进行排序

result = custom_obj_list.stream()
    .sorted((x1,x2) -> {
        return -x1.obj_function().compareTo(x2.obj_function());
    }).filter((x2) -> { 
        return x2.obj_function <= 1; 
    }).collect(Collectors.toList());
result=custom_obj_list.stream()
.已排序((x1,x2)->{
返回-x1.obj_函数().compareTo(x2.obj_函数());
}).filter((x2)->{
return x2.obj_函数如果只需要默认排序顺序,可以使用替代lambda表达式

result = custom_obj_list.stream()
                        .sorted(Comparator.comparingDouble(X1::obj_function))  //X1 should be class name
                        .filter(x2 -> x2.obj_function() <= 1)
                        .collect(Collectors.toList());
result = custom_obj_list.stream()
                        .sorted(Comparator.comparingDouble(X1::obj_function).reverse()))   ////X1 should be class name
                        .filter(x2 -> x2.obj_function() <= 1)
                        .collect(Collectors.toList());
result=custom_obj_list.stream()
.sorted(Comparator.comparingDouble(X1::obj_函数))//X1应该是类名

.filter(x2->x2.obj_函数()x2.obj_函数()->x+y
无法解析方法…
您遗漏了最重要的部分。
比较
–您确定吗?可能想进一步研究一下这个问题。您正在使用
对象函数
作为方法和字段。谢谢,我确实看到他提到了它作为方法,并根据@flakesCome更新了代码其中,这可能是OP的问题。我打赌他们对
obj_函数的定义实际上是
int
,导致方法解析错误lolIf也可以是
Integer
,但如果是
int
,他可以使用
https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparingInt-java.util.function.ToIntFunction-
@flakes我会等他回来,这是一个
,什么
obj_函数
返回