September 2019
Intermediate to advanced
816 pages
18h 47m
English
The findAny() method returns an arbitrary (nondeterministic) element from the stream. For example, the following snippet of code will return an element from the preceding list:
Optional<String> anyMelon = melons.stream() .findAny();if (!anyMelon.isEmpty()) { System.out.println("Any melon: " + anyMelon.get());} else { System.out.println("No melon was found");}
We can combine findAny() with other operations as well. Here's an example:
String anyApollo = melons.stream() .filter(m -> m.equals("Apollo")) .findAny() .orElse("nope");
This time, the result will be nope. There ...