3
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?

Naman
  • 27,789
  • 26
  • 218
  • 353
zhuochen shen
  • 1,673
  • 4
  • 16
  • 24
  • Specifying the type of `r` seem to solve the problem `.sorted(Comparator.comparingDouble((R r) -> r.getA() + r.getB()).reversed());`. Alternatively `.sorted(Comparator.comparingDouble(r -> r.getA() + r.getB()).reversed());` also seem to compile. – Venkata Raju Dec 17 '18 at 10:26

2 Answers2

3

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • 2
    On a side note, the [precision of adding doubles](https://stackoverflow.com/questions/322749/retain-precision-with-double-in-java) would still be a question that you shall look into. – Naman Dec 17 '18 at 05:36
  • 2
    IMHO, the link in that comment does not explain it enough. I'd recommend reading Knuth's algorithms (was it Knuth after all?) how important it is to start adding up with the smaller numbers and then adding the larger ones. – glglgl Dec 17 '18 at 05:50
0

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