Java Reference
In-Depth Information
noneMatch
The opposite of allMatch is noneMatch. It ensures that no elements in the stream match the
given predicate. For example, you could rewrite the previous example as follows using
noneMatch:
boolean isHealthy = menu.stream()
.noneMatch(d -> d.getCalories() >= 1000);
These three operations, anyMatch, allMatch, and noneMatch, make use of what we call
short-circuiting , a stream version of the familiar Java short-circuiting && and || operators.
Short-circuiting evaluation
Some operations don't need to process the whole stream to produce a result. For example, say
you need to evaluate a large boolean expression chained with and operators. You need only find
out that one expression is false to deduce that the whole expression will return false, no matter
how long the expression is; there's no need to evaluate the entire expression. This is what
short-circuiting refers to.
In relation to streams, certain operations such as allMatch, noneMatch, findFirst, and findAny
don't need to process the whole stream to produce a result. As soon as an element is found, a
result can be produced. Similarly, limit is also a short-circuiting operation: the operation only
needs to create a stream of a given size without processing all the elements in the stream. Such
operations are useful, for example, when you need to deal with streams of infinite size, because
they can turn an infinite stream into a stream of finite size. We show examples of infinite
streams in section 5.7 .
5.3.3. Finding an element
The findAny method returns an arbitrary element of the current stream. It can be used in
conjunction with other stream operations. For example, you may wish to find a dish that's
vegetarian. You can combine the filter method and findAny to express this query:
Optional<Dish> dish =
Search WWH ::




Custom Search