java8 stream findfirst().get()空指针
List<Integer> a = Arrays.asList(1, 2, 3);
Integer b = a.stream().filter(x -> x.equals(4)).findFirst().get();
System.out.println(b);
Exception in thread “main” java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:135)
解决方法:将.get()替换为.orElse(null),这样就会返回null值了。