Java常用方法-集合工具类常用方法 发表于 2019-01-23 | 更新于: 2019-09-25 | 分类于 Java常用方法 | 阅读量 字数统计: 174 | 阅读时长 ≈ 1 真香~ 对象List按属性排序1234567891011121314151617181920212223242526272829303132333435363738394041class Animal{ private String kind; private int quantity; public Animal(String k,int q){ this.kind = k; this.quantity = q; }}public class Main{ public static void main(String[] args) { Animal dog = new Animal("狗",10000); Animal cat = new Animal("猫",5000); Animal pig = new Animal("猪",2000); List<Animal> animalList = new ArrayList<Animal>(); animalList.add(dog); animalList.add(cat); animalList.add(pig); Collections.sort(animalList, new Comparator<TestA>() { @Override public int compare(TestA o1, TestA o2) { //升序 return o1.getAge().compareTo(o2.getAge()); } }); Collections.sort(animalList, new Comparator<TestA>() { @Override public int compare(TestA o1, TestA o2) { //降序 return o1.getAge().compareTo(o2.getAge()); } }); }} List初始化时进行赋值1List<Long> ids = Arrays.asList(98765432109L,12345678901L);