0
点赞
收藏
分享

微信扫一扫

Java - Lambda 之 stream().collect(Collectors.toList()) 空指针 NPE


问题

举例这段代码,在 collect 之前不仅 SupplierHistoryPO 不能为 null,而且 getOrder 也不允许为 null,否则报错NPE

List<SupplierRowDTO> rows = histories.stream()
.filter(h -> h.getAmount() > 0)
.sorted(Comparator.comparingInt(SupplierHistoryPO::getOrder))
.collect(Collectors.toList());


解决

可以在 filter 里对 SupplierHistoryPO & SupplierHistoryPO::getOrder 都需要判空过滤,否则很危险!


举报

相关推荐

0 条评论