【发布时间】:2020-05-05 07:53:36
【问题描述】:
目前我这样做:
List<MyObj> nullableList = myObjs.stream().filter(m -> m.isFit()).collect(Collectors.toList());
if (nullableList.isEmpty()) {
nullableList = null;
}
有更好的方法吗?像 Collectors.toListOrNullIfEmpty() 这样的东西?
【问题讨论】:
-
将 null 用作合法值并不是一个好习惯。特别是,null 绝不应用作空数组、集合或映射的同义词。使用空列表将允许其他代码省略空检查。
标签: java filter java-8 java-stream collectors