Comparator 排序报 空指针异常
作者:互联网
List<User> userList = new ArrayList<>(); userList.sort(Comparator.comparing(User::getEmployeeId)) ;
当列表中含有学号为空的元素时,会报空指针异常,
可采用
Comparator.nullsLast(String::compareTo)
解决元素的值为空的问题
结果为
userList.sort(Comparator.comparing(User::getEmployeeId, Comparator.nullsLast(String::compareTo)));
标签:String,getEmployeeId,comparing,userList,User,Comparator,排序,指针 来源: https://www.cnblogs.com/HS-WXL/p/11956910.html