1. lamdba中使用method refenrence对list集合进行排序
1. 基础数据类型排序
//初始化集合
List<Integer> list = Lists.list(1, 2, 3, 4);
//升序排序
Comparator.sort(list,new Comparator<Integer>(){
@Override
public int compare(Integer o1,Integer o2){
return o1.compareTo(o2); //降序同理
}
});
list.forEach(System.out::println)
//优化之后
Collections.sort(list, (o1, o2) -> o1.compareTo(o2));
//升序优化最后的效果
Collections.sort(list, Integer::compareTo);
//降序优化最后的效果
Collections.sort(list, Comparator.reverseOrder());
2. 对象排序
//初始化集合对象数据
List<Status> studentList = Lists.list(new Student("张三","男","10"),new Student("李四","女","20"),new Student("王五","男","30"));
//升序排序
Collections.sort(studentList,new Comparator<Student>(){
@Override
public int compare(Student o1,Student o2){
return o1.getSage().compareTo(o2.getSage());//降序同理
}
});
//升序排序优化后
Collections.sort(studentList,(Comparator.comparing(Student::getSage)));
//降序排序优化后
collections.sort(studentList,(o1.getSage(),o2.getSage()) -> o2.getSage().comparing(o1.getSage()))