Data Search JAVA em Ingles
Data Search JAVA em Ingles
Chapter FIFTEEN
Data Search
Exam Objectives
Search for data by using search methods of the Stream classes including findFirst, findAny,
anyMatch, allMatch, noneMatch.
Optional<T> findAny()
Optional<T> findFirst()
That search for an element in a stream. Since there's a possibility that an element
couldn't be found (if the stream is empty, for example), the return type of this methods
is an Optional .
That indicate if a certain element matches the given predicate, that's why they return a
boolean .
Since all these methods return a type different than a stream, they are considered
TERMINAL operations.
Of course, you can combine these methods with other stream operations:
When working with parallel streams, it's harder to find the first element. In this case, it's
better to use findAny() if you don't really mind which element is returned.
If the stream is empty or if there's no matching element, this method returns false :
allMatch() returns true only if ALL elements in the stream match the given predicate:
If the stream is empty, this method returns TRUE without evaluating the predicate:
If the stream is empty, this method returns also TRUE without evaluating the predicate:
Short-circuiting
All of these operations use something similar to the short-circuiting of && and ||
operators.
In the case of the find* operations, it's obvious that they stop at the first found element.
But in the case of the *Match operations, think about it, why would you evaluate all the
elements of a stream when by evaluating the third element (for example) you can
know if all or none (for example) of the elements will match?
The output:
Filter:1
Filter:2
AllMatch:2
Filter:3
Filter:4
AllMatch:4
false
As you can see, first of all, operations on a stream are not evaluated sequentially (in
this case, first filter all the elements and then evaluate if all elements match the
predicate of allMatch() ).
Second, we can see that as soon as an element passes the filter predicate (like 2 ) the
predicate of allMatch() is evaluated.
Just remember:
Key Points
The Stream API has two types of operation for searching. Methods starting with
Find:
Optional<T> findAny()
Optional<T> findFirst()
And method ending with Match:
boolean allMatch(Predicate<? super T> predicate)
boolean anyMatch(Predicate<? super T> predicate)
boolean noneMatch(Predicate<? super T> predicate)
Both types are considered TERMINAL operations.
findAny() and findFirst() practically do the same, they return the first element
they find in a stream. If the stream is empty, they return an empty Optional .
When working with parallel streams, it's harder to find the first element, so in this
case, it's better to use findAny() if you don't really mind which element is
returned.
anyMatch() returns true if any of the elements in a stream matches the given
predicate. If the stream is empty or if there's no matching element, it returns
false .
allMatch() returns true only if ALL elements in the stream match the given
predicate.
noneMatch() returns true if NONE of the elements in the stream match the given
predicate.
Both allMatch() and noneMatch() return true if the stream is empty.
All of these operations are short-circuiting, meaning that the evaluation stops
once a result is found.
Self Test
1. Given:
2. Which of the following methods of the Stream interface returns an Optional type?
A. filter()
B. findMatch()
C. findAny()
D. anyMatch()
3. Given:
4. Given:
Contact me