Java 8避免Lambda表达式NullPointException的方法
方法一
对于字符串的比较,使用非null的字符串的比较方法。
projects
  .stream()
  .filter(project -> "Completed".equalsIgnoreCase(project.getStatus()))
  .collect(Collectors.toList());
不建议使用project.getStatus().equalsIgnoreCase("Completed"),以避免status为null
方法二
使用filter把null的数据过滤掉
projects
  .stream()
  .filter(project -> project.getStatus() != null)
  .filter(project -> project.getStatus().equalsIgnoreCase("Completed"))
  .collect(Collectors.toList());
方法三
在使用前判断null值
personList
  .stream()
  .filter(project -> project.getStatus() != null && project.getStatus().equalsIgnoreCase("In Progress"))
  .collect(Collectors.toList());
 
             
             
             
             
            