anyMatch() in Java 8
In Java 8, anyMatch() is a method defined in the Stream interface. It performs a short-circuiting terminal operation. In this section, we will discuss the anyMatch() method in Java 8 Stream with an example. Before moving to the point, first, we will understand the intermediate and terminal operations in detail.
In Java 8 stream, the stream operations are divided into intermediate (like Stream.filter, Stream.map) and terminal (like Stream.forEach, Steam.reduce) operations. To get the desired results, we need to combine these operations that form stream pipelines.
Intermediate operations are lazy (means, does not perform the actual operation, instead return a new stream) in nature. It means, retaining the elements of the initial stream and always return a new stream of the initial stream. Note that the execution of the pipeline does not begin until the terminal operation of the pipeline is executed.
Intermediate operations are further divided into stateless operations (like filter and map) and stateful operations (like distinct and sorted).
The stateless operations do not retain the state from previously seen elements while processing a new element. Each element can be processed independently of operations on other elements.
The stateful operations retain the state from formerly seen elements while processing the new elements. The property of the stateful operation is that it processes the entire input before producing a result.
Suppose, we are performing the sorting operation over the stream and want to produce the result from that stream without seeing all elements of the stream which is not possible. Therefore, until one does not see all the elements of the stream, one cannot produce the result. Hence, we need to process the entire input before producing the result.
Terminal operations traverse the stream to produce a result or side-effect. As soon as the terminal operation is executed, the stream pipeline is considered consumed. After consumption, it can no longer be used. In case, we require to traverse the same data source (stream), return the stream to get a new stream.
Almost all the terminal operations are eager in nature. It means, they traverse the stream and process the pipeline before returning. Note that it is not allowed in two-terminal operations i.e. iterator() and spliterator().
Besides the above operations, there is another operation known as short-circuiting operation. Both intermediate and terminal operations may short-circuit.
An intermediate operation is short-circuiting if, there is a stream with infinite input. It may produce a finite stream as a result.
A terminal operation is short-circuiting if, there is a stream with infinite input. It may terminate in a finite time.
We observe that using a short-circuiting operation in the pipeline is essential but not sufficient. There is a condition to process an infinite stream that is terminate operation normally in finite time.
Java 8 Stream.anyMatch()
It returns whether any elements of this stream match the provided predicate. It may not evaluate the predicate on all elements if not necessary for determining the result.
Parameters: It accepts a non-interfering and stateless predicate that is to be applied to elements of the input stream.
Returns: It returns true if any element matches the specified predicate, else returns false. Returns false, if the stream is empty. In the case of an empty stream, the predicate is not evaluated.
Java anyMatch() Example
The following example depicts an empty stream always returns false.
Let's see another Java program in which we have parsed multiple predicates.
To satisfy multiple conditions, create a composed predicate with two or more simple predicates. In the given example, we have a list of Employees. We want to check if there is an employee whose age is 28 and the name starts with the alphabet R. The following Java program depicts the same.
true true false
The Stream interface provides another method for matching the specified predicate i.e. allMatch(). Difference between the allMatch() and the anyMatch() is that anyMatch() returns true if any of the elements in a stream matches the specified predicate. When using allMatch(), all the elements must match the given predicate.
Therefore, anyMatch() method can be used in the certain case when we want to check if there is at least one element in the stream. The contains() method of the List class also performs the same operation. So, we can also use the contains() method in place of anyMatch() method.
Hence, there is no difference between List.contains() and Stream.anyMatch() method.