Java Reference
In-Depth Information
In this chapter, you'll have an extensive look at the various operations supported by the Streams
API. These operations will let you express complex data processing queries such as filtering,
slicing, mapping, finding, matching, and reducing. Next, we'll explore special cases of streams:
numeric streams, streams built from multiple sources such as files and arrays, and finally
infinite streams.
5.1. Filtering and slicing
In this section, we look at how to select elements of a stream: filtering with a predicate, filtering
only unique elements, ignoring the first few elements of a stream, or truncating a stream to a
given size.
5.1.1. Filtering with a predicate
The Streams interface supports a filter method (which you should be familiar with by now). This
operation takes as argument a predicate (a function returning a boolean) and returns a stream
including all elements that match the predicate. For example, you can create a vegetarian menu
by filtering all vegetarian dishes as follows and as illustrated in figure 5.1 :
Figure 5.1. Filtering a stream with a predicate
 
Search WWH ::




Custom Search