Find first matched item
Description
The following code shows how to find first matched item.
Example
/*from w w w . j a v a 2 s . c o m*/
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class Main {
public static void main(String[] args) {
List<String> names = Arrays.asList("XML", "HTML", "Java", "Javascript", "CSS", "Static");
Optional<String> firstS = names.stream().filter(name -> name.startsWith("S")).findFirst();
System.out.println(firstS.orElse("None found"));
}
}
The code above generates the following result.