// 填充数据
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());
版权声明:本文为RunWithSmile原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。