![](/img/trans.png)
[英]java 8 stream for sorting a list of objects based on particular field
[英]Java stream sorting objects based on the 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());
但我正在努力解决以下错误消息
stream().sorted() 无法解析符号或无法解析方法
您可以使用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());
或反向排序
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.