sorted in stream is not applicable
java stream filter
java-stream sort objects by field
stream sorted reverse
java stream map
stream sorted comparator not working
stream sorted by attribute
java stream sort by multiple fields
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>)
sorted in stream is not applicable, sorted in stream is not applicable. java stream sorted java-stream sort objects by field sorted method in stream api java stream filter java stream sort by date For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements. In java 8, Comparator can be instantiated using lambda expression. We can also reverse the natural ordering as well as ordering provided by Comparator.
.map(a -> a.getPurchases()), you appear to be expecting a
Stream<Purchase>, but what you really have is a
Stream<Purchase> is indeed what you want, instead you should use
.flatMap(a -> a.getPurchases().stream())
Stream sorted (Comparator comparator) method in Java , For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may incorporate Few examples to show you how to sort a List with stream.sorted() 1. List. 1.1 Sort a List with Comparator.naturalOrder()
The a.getPurchases() gives you a set, and your comparator is comparing elements of the set, not sets.
Based on your expected output, I understand you want to get the set which has the latest purchase date. If each set contains only same purchase date purchases, you can create your comparator like this:
.sorted(Comparator.comparing(purchases -> purchases.iterator().next(), (p1, p2) -> -p1.compareTo(p2)));
If purchase dates differ inside a set, you need to get max (or min) purchase date inside a set, then compare that in between the sets, something like:
final Stream<Set<Purchase>> sorted = acquisitions.stream() .map(Acquisition::getPurchases) .sorted(Comparator.comparing(purchases -> Collections.max(purchases, Comparator.comparing(Purchase::getPurchaseDate)).getPurchaseDate(), (date1, date2) -> -date1.compareTo(date2)));
Stream (Java Platform SE 8 ), By contrast, streams do not provide a means to directly access or manipulate Returns a stream consisting of the elements of this stream, sorted according to Parameters: predicate - a non-interfering, stateless predicate to apply to each On this page we will provide java 8 Stream sorted() example. We can sort the stream in natural ordering as well as ordering provided by Comparator. In java 8 Comparator can be instantiated using lambda expression. We can also reverse the natural ordering as well as ordering provided by Comparator.
Try doing it this way:
Set<Purchase> purchases = user.getAcquisitions() .parallelStream() .map(Acquisition::getPurchases) .flatMap(Set::stream) .collect(Collectors.toCollection(TreeSet::new));
New York Court of Appeals. Records and Briefs.: 84 NY2D 430, , To create a sorted output tape file. overview: This program needs the temporary in step - 1 of the monthly job stream (ACOOMRB1) reading and sorting all records. OUTPUT REPORTS Not applicable - DISPLAYS, MESSAGES: Total record Java 8 Stream. Java Stream API Example Tutorial. java.util.stream, Java Stream reduce, map, collect, sorted, flatMap, filter, forEach, match, findFirst
Middleware 2006: ACM/IFIP/USENIX 7th International Middleware , Sort-merge-join based approaches assume that the inputs are structurally sorted in The filtered and matched result, R, consists of a stream of pairs, 〈rmsgi ,rlocj 〉, where which are shown to work well for static XML data, are not applicable. Throughout the article, we'll be sorting above Employee array using different functions. For test assertions, we'll use a set of pre-sorted arrays that we will compare to our sort results (i.e., the employees array) for different scenarios. Let's declare a few of these arrays:
Package java.util.stream, A stream is not a data structure that stores elements; instead, it conveys desired characteristics without examining all of the strings available from the source. For example, one cannot produce any results from sorting a stream until one has Pointer sorting. The approach we are using is known in the classical literature as pointer sorting, so called because we process references to keys and do not move the data itself. Keys are immutable. It stands to reason that an array might not remain sorted if a client is allowed to change the values of keys after the sort.
Comparators and Collectors, The sorted method on Stream produces a new, sorted stream using the natural ordering for the class. This approach does not follow the functional principles supported by Java 8, which return collector . finisher ( ) . apply ( container ) ; 3. 1. Comparator detaches the sorting logic from your object and contains the compareTo() logic within your sort() method. Using Comparator with an anonymous inner class In this next example, we use an anonymous inner class to compare the value of objects.
- Just a question about your object model: why could an acquisition have multiple purchases? To me the two words mean more or less the same thing.
- Legacy issues :-(
- I understand, and now it makes sense why you're in that mess.
- in fact I would like a Set<Purchase>
- Just collect your stream into a set then. But why do you sort your stream if you want a set? A Set is unordered so it makes no sense to sort it before collecting.
- @Ricola very good point indeed!! or if they really want a set with respect to the ordering then they can do
- If that is the case, then you need to decide how to order two sets of purchases. I cannot help you with this.