java8中使用流时,如何重写Comparable接口的compare方法
java8中使用流时,如何重写Comparable接口的compare方法
前言
重写所有接口都可以使用这种方式
List<类> models = 集合.stream().sorted( (a,b) -> {
if (a == null && b == null) {
return 0;
}
if (a == null) {
return -1;
}
if (b == null) {
return 1;
}
if (a.get方法() == null) {
return -1;
}
if (b.get方法() == null) {
return 1;
}
return a.get方法().compareTo(b.get方法());
}).collect(Collectors.toList());