Java Reference
In-Depth Information
17.4.2 Sorting a Stream and Collecting the Results
In Section 7.15, you learned how to sort arrays with the sort and parallelSort static
methods of class Arrays . You'll often sort the results of stream operations, so in lines 18-
21 we'll sort the values array using stream techniques and display the sorted values. First,
line 19 creates a Stream<Integer> from values . Next, line 20 calls Stream method sort-
ed which sort the elements—this results in an intermediate Stream<Integers> with the
values in ascending order.
To display the sorted results, we could output each value using Stream terminal oper-
ation forEach (as in line 15 of Fig. 17.5). However, when processing streams, you often
create new collections containing the results so that you can perform additional operations
on them. To create a collection, you can use Stream method collect (Fig. 17.6, line 21),
which is a terminal operation . As the stream pipeline is processed, method collect performs
a mutable reduction operation that places the results into an object which subsequently can
be modified —often a collection, such as a List , Map or Set . The version of method collect
in line 21 receives as it's argument an object that implements interface Collector (package
java.util.stream ), which specifies how to perform the mutable reduction. Class Collec-
tors (package java.util.stream ) provides static methods that return predefined Col-
lector implementations. For example, Collectors method toList (line 21) transforms
the Stream<Integer> into a List<Integer> collection. In lines 18-21, the resulting
List<Integer> is then displayed with an implicit call to its toString method.
We demonstrate another version of method collect in Section 17.6. For more
details on class Collectors , visit:
http://download.java.net/jdk8/docs/api/java/util/stream/
Collectors.html
17.4.3 Filtering a Stream and Storing the Results for Later Use
Lines 24-27 of Fig. 17.6 create a Stream<Integer> , call Stream method filter (which
receives a Predicate ) to locate all the values greater than 4 and collect the results into a
List<Integer> . Like IntPredicate (Section 17.3.4), functional interface Predicate has
a test method that returns a boolean indicating whether the argument satisfies a condi-
tion, as well as methods and , negate and or .
We assign the stream pipeline's resulting List<Integer> to variable greaterThan4 ,
which is used in line 28 to display the values greater than 4 and used again in lines 40-42
to perform additional operations on only the values greater than 4.
17.4.4 Filtering and Sorting a Stream and Collecting the Results
Lines 31-35 display the values greater than 4 in sorted order. First, line 32 creates a
Stream<Integer> . Then line 33 filter s the elements to locate all the values greater than
4. Next, line 34 indicates that we'd like the results sorted . Finally, line 35 collect s the
results into a List<Integer> , which is then displayed as a String .
17.4.5 Sorting Previously Collected Results
Lines 40-42 use the greaterThan4 collection that was created in lines 24-27 to show ad-
ditional processing on a collection containing the results of a prior stream pipeline. In this
 
 
 
 
 
Search WWH ::




Custom Search