stream.findFirst() / anyMatch() / allMatch() / noneMatch()
These are terminal operations for searching a stream for elements that match a condition, or for checking whether all elements satisfy a condition (Java 8+). findFirst() returns the first matching element, while anyMatch() checks whether at least one element satisfies the condition.
Syntax
// Returns the first element as an Optional. stream.findFirst(); // Returns any element as an Optional (intended for parallel streams). stream.findAny(); // Checks whether any element satisfies the condition. stream.anyMatch(element -> condition); // Checks whether all elements satisfy the condition. stream.allMatch(element -> condition); // Checks whether no elements satisfy the condition. stream.noneMatch(element -> condition);
Method List
| Method | Description |
|---|---|
| findFirst() | Returns the first element of the stream as an Optional. Returns an empty Optional if the stream is empty. |
| findAny() | Returns any element of the stream as an Optional. In a parallel stream, returns the first element found. |
| anyMatch(Predicate) | Checks whether any element satisfies the condition, and returns a boolean. |
| allMatch(Predicate) | Checks whether all elements satisfy the condition, and returns a boolean. |
| noneMatch(Predicate) | Checks whether no elements satisfy the condition, and returns a boolean. |
Sample Code
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
// Find the first name that starts with 'C'.
Optional<String> first = names.stream()
.filter(name -> name.startsWith("C"))
.findFirst();
System.out.println(first.orElse("Not found")); // Prints "Charlie".
// Check whether any name has 5 or more characters.
boolean hasLong = names.stream()
.anyMatch(name -> name.length() >= 5);
System.out.println(hasLong); // Prints "true" (Alice, Charlie, David).
// Check whether all names have 3 or more characters.
boolean allLong = names.stream()
.allMatch(name -> name.length() >= 3);
System.out.println(allLong); // Prints "true".
// Check whether no names contain digits.
boolean noDigit = names.stream()
.noneMatch(name -> name.matches(".*\\d.*"));
System.out.println(noDigit); // Prints "true" (no names contain digits).
// Handle the case where findFirst returns an empty Optional.
Optional<String> empty = names.stream()
.filter(name -> name.startsWith("Z"))
.findFirst();
System.out.println(empty.isPresent()); // Prints "false".
Notes
Because findFirst() returns an Optional, you can handle the case where no element is found safely. Calling get() directly throws an exception if the element is absent, so use orElse() or isPresent() to retrieve the value safely.
anyMatch(), allMatch(), and noneMatch() use short-circuit evaluation. For example, anyMatch() stops evaluating the remaining elements as soon as it finds one that satisfies the condition, making it efficient.
For details on the Optional class, see Optional.of() / isPresent() / orElse().
If you find any errors or copyright issues, please contact us.