findFirst() — returns the first element in encounter order. findAny() — returns any element (nondeterministic). Think of findFirst() as the first person in line, while findAny() is whichever person happens to be closest.
findFirst():
Optional<T> with the first element.1Optional<String> first = list.stream()2 .filter(s -> s.length() > 3)3 .findFirst();
findAny():
Optional<T> with any element.1Optional<String> any = list.parallelStream()2 .filter(s -> s.length() > 3)3 .findAny();
When to use:
1// findFirst() when order matters2String first = list.stream()3 .filter(s -> s.startsWith("A"))4 .findFirst()5 .orElse("none");67// findAny() when any match is fine8String any = list.parallelStream()9 .filter(s -> s.startsWith("A"))10 .findAny()11 .orElse("none");
Performance considerations: