Java 求和后流排序反转

Java stream sort reversed after sum

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() 进行排序并将其反转?

一种方法是将 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 .

这是同一个问题的三种解法

豆子Class

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 + "]";
}
}

Driver Class

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]

Here is complete picture of program with output