I have this list that I want to order in reserve order, but I didn't find any .reversed()
function in autocomplete assist
myMenus(user)
.stream()
.filter(mps -> mps.get1PercentageChange() > 0 &&
mps.get2PercentageChange() > 0 &&
mps.get3PercentageChange() > 0 &
mps.get4PercentageChange() > 0)
.sorted(comparing(mps -> mps.getDailyPercentageChange()))
.collect(toList());
I have also tried:
myMenus(user)
.stream()
.filter(mps -> mps.get1PercentageChange() > 0 &&
mps.get2PercentageChange() > 0 &&
mps.get3PercentageChange() > 0 &
mps.get4PercentageChange() > 0)
.sorted(comparing(mps -> mps.getDailyPercentageChange()).reversed())
.collect(toList());
but then I have the compilation error:
Cannot infer type argument(s) for <T, U> comparing(Function<? super T,?
extends U>)
Java stream provides a method sorted() which can sort List in ascending and descending order without you to write any complex logic. By default sorting() method sorts the list in ascending order. If we want to sort in descending order we need to pass Comparator. reverseOrder() in sorted() method as a parameter.
To get the list in the reverse order, we need to use Collections. reverseOrder() and Collections. sort() methods together.
Stream sorted() in Java Stream sorted() returns a stream consisting of the elements of this stream, sorted according to natural order. For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed.
It's a type inference issue. You'll need to help the compiler out.
few things you could try:
.sorted(comparing(T::getDailyPercentageChange).reversed())
or
.sorted(comparing((T mps) -> mps.getDailyPercentageChange()).reversed())
Where T
is the type of elements being compared.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With