[英]Java stream sorting objects based on the double value
I have list of X1
objects and trying to sort the list based on method obj_function()
which returns Double
value.我有
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());
But I'm struggling to solve the below error message但我正在努力解决以下错误消息
stream().sorted() cannot resolve symbol or cannot resolve method
stream().sorted() 无法解析符号或无法解析方法
You can use the Comparator.comparing instead of lambda expression, If you just want default sorting order您可以使用Comparator.comparing代替 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());
or reverse sorting order或反向排序
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.