Java stream sort reversed after sum

Viewed 319
list
.parallelStream()
.sorted(Comparator.comparingDouble(r -> r.getA() + r.getB()).reversed())

get a compile error, cannot resolve method r.getA(), but

list
.parallelStream()                                    
.sorted(Comparator.comparingDouble(r -> r.getA() + r.getB())

is ok,

so how can I do a sort by r -> r.getA() + r.getB() and get it reversed?

2 Answers

One way to do that would be to type cast the ToDoubleFunction as:

list.parallelStream()
    .sorted(Comparator.comparingDouble((ToDoubleFunction<C>) r -> r.getA() + r.getB())
            .reversed());

Though I would have preferred explicitly creating a Comparator<C> as

list.parallelStream()
        .sorted(((Comparator<C>) (o1, o2) -> Double.compare(o1.getA() + o1.getB(), o2.getA() + o2.getB()))
                .reversed());

Note: C here is your object type, which constitutes the list in question as well.

Here Is the three solutions of same problem.

Bean 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);
 }

Output.

Without Sorting...

Item [lPrice=1.0, hPrice=0.0]

Item [lPrice=3.0, hPrice=0.0]

Item [lPrice=4.0, hPrice=0.0]

Item [lPrice=6.0, hPrice=0.0]

Item [lPrice=5.0, hPrice=0.0]

Item [lPrice=7.0, hPrice=0.0]

=== in descending order===

===Sort based on sum of lower price and higher price of item===

Item [lPrice=7.0, hPrice=0.0]

Item [lPrice=6.0, hPrice=0.0]

Item [lPrice=5.0, hPrice=0.0]

Item [lPrice=4.0, hPrice=0.0]

Item [lPrice=3.0, hPrice=0.0]

Item [lPrice=1.0, hPrice=0.0]

=== Another Sol'n in descending order===

===Sort based on sum of lower price and higher price of item===

Item [lPrice=7.0, hPrice=0.0]

Item [lPrice=6.0, hPrice=0.0]

Item [lPrice=5.0, hPrice=0.0]

Item [lPrice=4.0, hPrice=0.0]

Item [lPrice=3.0, hPrice=0.0]

Item [lPrice=1.0, hPrice=0.0]

===Here is one more Solution===

Item [lPrice=7.0, hPrice=0.0]

Item [lPrice=6.0, hPrice=0.0]

Item [lPrice=5.0, hPrice=0.0]

Item [lPrice=4.0, hPrice=0.0]

Item [lPrice=3.0, hPrice=0.0]

Item [lPrice=1.0, hPrice=0.0]

Here is complete picture of program with output

Related