I have this piece of Java8 code:
Set<Purchase> purchases =
user.getAcquisitions()
.parallelStream()
.map(a -> a.getPurchases())
.sorted(Comparator.comparing(Purchase::getPurchaseDate).reversed());
But I have this compilation error and I don't know why:
The method sorted(Comparator<? super Set<Purchase>>) in the type Stream<Set<Purchase>> is not applicable for the arguments
(Comparator<Purchase>)
After .map(a -> a.getPurchases())
, you appear to be expecting a Stream<Purchase>
, but what you really have is a Stream<Set<Purchase>>
.
If a Stream<Purchase>
is indeed what you want, instead you should use
.flatMap(a -> a.getPurchases().stream())
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