// 填充数据
List<Long> testList = Lists.newArrayList(1L, null, 2L, 3L);
// 过滤数据,如果不加null 校验会报空指针异常 。因为null != 1 表达式在java中不成立所以必须先校验是否为null,当然如果筛选的源数据没有空值那可以不用校验
// 筛选出null值和非1数据
List<Long> filterList = testList.stream().filter(temp -> temp == null || temp != 1).collect(Collectors.toList());
// 筛选出非null且非1数据
List<Long> filterList = testList.stream().filter(temp -> temp != null && temp != 1).collect(Collectors.toList());
java8 stream().filter() 未加null校验导致空指针问题
最新推荐文章于 2024-06-20 08:45:14 发布
关键词由CSDN通过智能技术生成