... arrays or from ranges of elements in the arrays.

17.10.2 Sorting a Stream and Collecting the Results

The stream pipeline in lines 16–18


Arrays.stream(values)
      .sorted()
      .collect(Collectors.toList())

uses stream techniques to sort the values array and collect the results in a List<Integer>. First, line 16 creates a Stream<Integer> from values. Next, line 17 calls Stream method sorted to sort the elements—this results in an intermediate Stream<Integer> with the values in ascending order. (Section 17.11.3 discusses how to sort in descending order.)

Creating a New Collection Containing a Stream Pipeline’s Results

When processing streams, you often create new collections containing the results so that you can perform operations on them later. To ...

Get Java How To Program, Late Objects, 11th 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.