July 2019
Intermediate to advanced
536 pages
12h 57m
English
We can filter a stream using the filter() method. Let's filter the stream obtained from the languages list to filter items starting with E, as shown in the following code:
stream.filter( item -> item.startsWith("E") );
The filter() method takes a Predicate as a parameter. The predicate interface contains a function called boolean test(T t) that takes a single parameter and returns a boolean. In the preceding example, we passed the lambda expression item -> item.startsWith("E") to the test() function.
When the filter() method is called on a Stream, the filter passed as a parameter to the filter() function is stored internally. The items are not filtered immediately.
The parameter passed to the filter() function determines ...
Read now
Unlock full access