In this example we show java.util.Stream examples on finding whether some elements match a given property in a set of data through the allMatch, anyMatch, nonMatch, findFirst and findAny methods.
Setup
allMatch
The Stream.allMatch method returns whether all elements of a stream match the provided predicate. If the stream is empty then true is returned and the predicate is not evaluated. For example, you can use it to find if all the games name contains a letter 'a'.
anyMatch
The Stream.anyMatch method can be used to determine if any element in the stream matches a supplied predicate. If the stream is empty then false is returned and the predicate is not evaluated. For example, you can check if any game total plays is greater than 1000.
noneMatch
The opposite of Stream.allMatch, Stream.noneMatch will check whether no elements of a given stream match the provided predicate. If the stream is empty then true is returned and the predicate is not evaluated. In the example below, we will check if any game matches a rating of above 5 and if the total plays is greater than 100.
findFirst
Similar to guava's getFirst method, Stream.findFirst will return the first element of a given stream or will return an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned. In this example, we will simply call findFirst which returns the first element in the games collection.
findAny
Stream.findAny will return Optional describing some element in the stream or empty Optional if the stream is empty. In this example, we want to find any elements in the games collection that has a release date of April which there is none.