219.
a.
b.
220.
condition. It accepts as parameter a Predicate instance and
filters the elements in the stream
based on the specified
predicate. The following code demonstrates this:
Stream
numbers = Stream.of(7,3,9,6,1);
Stream output = numbers.filter(num–> num > 5);
This code creates a Stream of some Integer values. It then
invokes the filter() method with a lambda expression that
checks if the input number is greater than 5. So, the output
Stream will only consist of those numbers that are greater than 5.
Explain the types of Stream operations.
Answer:
Stream operations can be categorized as follows:
Intermediate: Intermediate operations operate on Streams
and produce a Stream output. Since intermediate operations
produce a Stream, they can be chained to perform a series of
operations. Some examples of intermediate operations are
filter, map, sorted
Terminal: Terminal operations operate on Streams but
produce a non–stream result. So, they can produce a result
of any data type. Terminal operations cannot be chained.
When a number of Stream operations are chained, a terminal
operation is typically the last operation. Some examples of
terminal operations are count, anyMatch, allMatch,
collect
Do'stlaringiz bilan baham: