? ? ? ? 有时候需要过滤集合中的元素是否包含指定的字符串,可以使用stream流式的方式优雅获取
String flag = "de";
Optional<String> first = Stream.of("abc", "bcd", "cde", "def", "fgk", "gkl").filter(s -> s.contains(flag)).findFirst();
/// 判断是否存在包含de的元素,取出第一个
if(first.isPresent()){
System.out.println("first have value ------->"+first.get());
}else {
System.out.println("first null value ");
}
/// 判断是否存在包含de的元素,取出任意一个
Optional<String> any = Stream.of("abc", "bcd", "cde", "def", "fgk", "gkl").filter(s -> s.contains(flag)).findAny();
if(any.isPresent()){
System.out.println("any have value ------->"+any.get());
}else {
System.out.println("any null value ");
}