list
.parallelStream()
.sorted(Comparator.comparingDouble(r -> r.getA() + r.getB()).reversed())
获取编译错误cannot resolve method r.getA()
,但
list
.parallelStream()
.sorted(Comparator.comparingDouble(r -> r.getA() + r.getB())
还可以,
那我怎样才能通过r-> r.getA()+ r.getB()进行排序并将其取反?
答案 0 :(得分:2)
一种方法是将ToDoubleFunction
强制转换为:
list.parallelStream()
.sorted(Comparator.comparingDouble((ToDoubleFunction<C>) r -> r.getA() + r.getB())
.reversed());
尽管我希望显式创建一个Comparator<C>
作为
list.parallelStream()
.sorted(((Comparator<C>) (o1, o2) -> Double.compare(o1.getA() + o1.getB(), o2.getA() + o2.getB()))
.reversed());
注意 :C
是您的对象类型,它也构成了相关的list
。
答案 1 :(得分:0)
这是相同问题的三个解决方案。
Bean类
Item.java
public Item {
double lPrice;
double hPrice;
/**
* @param lPrice
* @param hPrice
*/
public Item(double lPrice, double hPrice) {
super();
this.lPrice = lPrice;
this.hPrice = hPrice;
}
public double getlPrice() {
return lPrice;
}
public void setlPrice(double lPrice) {
this.lPrice = lPrice;
}
public double gethPrice() {
return hPrice;
}
public void sethPrice(double hPrice) {
this.hPrice = hPrice;
}
@Override
public String toString() {
return "Item [lPrice=" + lPrice + ", hPrice=" + hPrice + "]";
}
}
驱动程序类
public static void main(String[] args) {
List<Item> list = new ArrayList<>();
list.add(new Item(1.0, 0.0)); list.add(new Item(3.0, 0.0)); list.add(new Item(4.0, 0.0));
list.add(new Item(6.0, 0.0)); list.add(new Item(5.0, 0.0)); list.add(new Item(7.0, 0.0));
System.out.println("Without Sorting...");
list.forEach(System.out::println);
list.sort(Comparator.comparingDouble(i -> ((Item) i).getlPrice()+((Item) i).gethPrice() ).reversed());
System.out.println("\n=== in descending order===");
System.out.println("===Sort based on sum of lower price and higher price of item===");
list.forEach(System.out::println);
list.clear();
list.add(new Item(1.0, 0.0)); list.add(new Item(3.0, 0.0)); list.add(new Item(4.0, 0.0));
list.add(new Item(6.0, 0.0)); list.add(new Item(5.0, 0.0)); list.add(new Item(7.0, 0.0));
list = list.parallelStream().sorted(Comparator.comparingDouble(i -> ((Item) i).getlPrice()+((Item) i).gethPrice() ).reversed()).collect(Collectors.toList());
System.out.println("\n=== Another Sol'n in descending order===");
System.out.println("===Sort based on sum of lower price and higher price of item===");
list.forEach(System.out::println);
list.clear();
list.add(new Item(1.0, 0.0)); list.add(new Item(3.0, 0.0)); list.add(new Item(4.0, 0.0));
list.add(new Item(6.0, 0.0)); list.add(new Item(5.0, 0.0)); list.add(new Item(7.0, 0.0));
list.sort((a1,a2) -> {
double item1 = a1.getlPrice()+a1.gethPrice();
double item2 = a2.getlPrice()+a2.gethPrice();
return -Double.compare(item1, item2);
});
System.out.println("\n===Here is one more Solution===");
list.forEach(System.out::println);
}
输出。
没有排序...
项目[lPrice = 1.0,hPrice = 0.0]
项目[lPrice = 3.0,hPrice = 0.0]
项目[lPrice = 4.0,hPrice = 0.0]
项目[lPrice = 6.0,hPrice = 0.0]
项目[lPrice = 5.0,hPrice = 0.0]
项目[lPrice = 7.0,hPrice = 0.0]
===降序===
===基于商品的较低价格和较高价格之和排序===
项目[lPrice = 7.0,hPrice = 0.0]
项目[lPrice = 6.0,hPrice = 0.0]
项目[lPrice = 5.0,hPrice = 0.0]
项目[lPrice = 4.0,hPrice = 0.0]
项目[lPrice = 3.0,hPrice = 0.0]
项目[lPrice = 1.0,hPrice = 0.0]
===另一个Sol'n降序===
===基于商品的较低价格和较高价格之和排序===
项目[lPrice = 7.0,hPrice = 0.0]
项目[lPrice = 6.0,hPrice = 0.0]
项目[lPrice = 5.0,hPrice = 0.0]
项目[lPrice = 4.0,hPrice = 0.0]
项目[lPrice = 3.0,hPrice = 0.0]
项目[lPrice = 1.0,hPrice = 0.0]
===这里是另外一个解决方案===
项目[lPrice = 7.0,hPrice = 0.0]
项目[lPrice = 6.0,hPrice = 0.0]
项目[lPrice = 5.0,hPrice = 0.0]
项目[lPrice = 4.0,hPrice = 0.0]
项目[lPrice = 3.0,hPrice = 0.0]
项目[lPrice = 1.0,hPrice = 0.0]