17.4 Stream<Integer> Manipulations

[This section demonstrates how lambdas and streams can be used to simplify programming tasks that you learned in Chapter 7, Arrays and ArrayLists.]

Just as class IntStream’s method of can create an IntStream from an array of ints, class Array’s stream method can be used to create a Stream from an array of objects. Figure 17.6 performs filtering and sorting on a Stream<Integer>, using the same techniques you learned in Section 17.3. The program also shows how to collect the results of a stream pipeline’s operations into a new collection that you can process in subsequent statements. Throughout this example, we use the Integer array values (line 12) that’s initialized with int values—the compiler boxes each ...

Get Java™ How To Program (Early Objects), Tenth 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.