Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorted in stream is not applicable

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>)
like image 788
Nunyet de Can Calçada Avatar asked Dec 14 '22 13:12

Nunyet de Can Calçada


1 Answers

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())
like image 81
Joe C Avatar answered Dec 23 '22 17:12

Joe C