java8 lambda集合排序
作者:互联网
1.使用Comparator.comparing按照动物年龄升序/降序排序
public class Demo {
public static void main(String[] args) {
ArrayList<Animal> listAnimals = new ArrayList<>();
listAnimals.add(new Animal().setName("狗").setAge(2));
listAnimals.add(new Animal().setName("猫").setAge(3));
listAnimals.add(new Animal().setName("老鼠").setAge(1));
listAnimals.sort(Comparator.comparing(Animal::getAge));
listAnimals.forEach(System.out::println);
// 降序排序
// listAnimals.sort(Comparator.comparing(Animal::getAge).reversed());
}
}
标签:setName,listAnimals,comparing,add,Animal,new,排序,java8,lambda 来源: https://blog.csdn.net/m0_59259076/article/details/122791147