[英]How to avoid NPE in below case?
List<User> sortedList = userList.stream()
.sorted(Comparator.comparingInt(User::getAge).reversed())
.collect(Collectors.toList());
sortedList.forEach(System.out::println)
In the above case if User is null it is giving NPE.在上述情况下,如果 User 为 null,则表示为 NPE。 How to avoid NPE here?
如何在这里避免 NPE?
Yes I got the solution.是的,我得到了解决方案。 It is very easy.
这很容易。 Just use stream.
只需使用流。 filter(a->a!=null)...
过滤器(a->a!=null)...
List<User> sortedList = userList.stream()
.filter(a->a!=null)
.sorted(Comparator.comparingInt(User::getAge).reversed())
.collect(Collectors.toList());
sortedList.forEach(System.out::println)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.