List<Student> students = new ArrayList<>(); Student s1 = new Student("haha",12,"女",2); Student s2 = new Student("xixi",13,"女",4); Student s3 = new Student("guagua",14,"女",3); Student s4 = new Student("yaya",15,"女",1); students.add(s1); students.add(s2); students.add(s3); students.add(s4); students.sort(Comparator.comparing(Student :: getSort)); for (int i = 0; i < students.size(); i++) { Student s = students.get(i); System.out.println(s.getName() + s.getAge() + s.getSex() + s.getSort()); // yaya15女1 // haha12女2 // guagua14女3 // xixi13女4 }
【java】List元素排序Lambda表达式
最新推荐文章于 2024-07-22 19:19:31 发布