With Java 8+, you can easily find all elements of a collection that match a Predicate
.
someCollection.stream().filter(somePredicate)
You could then find the first element:
someCollection.stream().filter(somePredicate).findFirst()
The problem with this, though, is that it runs the Predicate
against all the elements. Is there a clean way to only run the Predicate
against elements until the first match is found, and then return it, like anyMatch
does (but returns a boolean
telling if one was found)?