Sorting the elements of a stream

Another typical operation you will want to do with a Stream is sorting its elements. For example, you may want to sort the elements of the Stream by name, postal code, or any other numeric value.

With streams, we have other considerations with the so-called encounter order. Some streams may have a defined encounter order (it depends on the source of the Stream). Some operations work with the elements of the stream in its encountered ordered, such as limit(), skip(), and others. This makes that parallel computation for this methods doesn't give us good performance. In these cases, you can speed-up the execution of these methods by deleting the ordering constraint.

In this recipe, you will learn how to sort ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.