September 2019
Intermediate to advanced
816 pages
18h 47m
English
If findAny() returns any element, findFirst() returns the first element from the stream. Obviously, this method is useful when we are interested only in the first element of a stream (for example, the winner of a contest should be the first element in a sorted list of competitors).
For now, let's assume that we want the first melon in the list:
Optional<String> firstMelon = melons.stream() .findFirst();if (!firstMelon.isEmpty()) { System.out.println("First melon: ...