- 使用Comparable接口对下列四位同学的 成绩 做 降序 排序,如果成绩一样,那在成绩排序的基础上按照 年龄 由 小 到 大 排序。
package com.csdn.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Collections; import java.util.TreeSet; @Data @NoArgsConstructor @AllArgsConstructor public class Student implements Comparable<Student> { private String name; private int age; private double score; @Override public int compareTo(Student stu) { if (this.getScore() > stu.getScore()) { return -1; } else if (this.getScore() < stu.getScore()) { return 1; } return this.getAge() - stu.getAge(); } public static void main(String[] args) { System.out.println("按照成绩和年龄排序:"); TreeSet<Student> set = new TreeSet<>(); Collections.addAll(set, new Student("d", 20, 90.0), new Student("c", 22, 90.0), new Student("b", 20, 99.0), new Student("a",22,100.0)); //自动调用Student中compareTo()方法 for (Student student : set) { System.out.println(student); } /* [Student(name=a,age=22, score=100.0), Student(name=b, age=20, score=99.0), Student(name=d, age=20, score=90.0), Student(name=c, age=22, score=90.0)] */ } }
- 使用Comparator实现按照 姓名 排序
package com.csdn.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Collections; import java.util.Comparator; import java.util.TreeSet; @Data @NoArgsConstructor @AllArgsConstructor public class Student{ private String name; private int age; private double score; public static void main(String[] args) { System.out.println("按照姓名排序:"); TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getName().compareTo(o2.getName()); } }); Collections.addAll(set, new Student("d", 20, 90.0), new Student("c", 22, 90.0), new Student("b", 20, 99.0), new Student("a",22,100.0)); for (Student student : set) { System.out.println(student); } /*按照姓名排序: Student(name=a, age=22, score=100.0) Student(name=b, age=20, score=99.0) Student(name=c, age=22, score=90.0) Student(name=d, age=20, score=90.0)*/ } }
- Comparable和Comparator混合使用
- Comparator的 优先级 高于Comparable
package com.csdn.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Collections; import java.util.Comparator; import java.util.TreeSet; @Data @NoArgsConstructor @AllArgsConstructor public class Student implements Comparable<Student>{ private String name; private int age; private double score; @Override public int compareTo(Student o) { if (this.getScore()>o.getScore()) { return -1; } else if (this.getScore()<o.getScore()) { return 1; } return this.getAge() - o.getAge(); } public static void main(String[] args) { System.out.println("按照成绩和年龄排序"); TreeSet<Student> set = new TreeSet<>(); Collections.addAll(set, new Student("d", 20, 90.0), new Student("c", 22, 90.0), new Student("b", 20, 99.0), new Student("a",22,100.0)); //自动调用Student中compareTo()方法 for (Student student : set) { System.out.println(student); } /* 按照成绩和年龄排序 Student(name=a, age=22, score=100.0) Student(name=b, age=20, score=99.0) Student(name=d, age=20, score=90.0) Student(name=c, age=22, score=90.0)*/ System.out.println("按照姓名排序:"); TreeSet<Student> all = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getName().compareTo(o2.getName()); } }); all.addAll(set); for (Student student : all) { System.out.println(student); } /*按照姓名排序: Student(name=a, age=22, score=100.0) Student(name=b, age=20, score=99.0) Student(name=c, age=22, score=90.0) Student(name=d, age=20, score=90.0)*/ } }
使用TreeSet集合存储元素,使用Comparable和Comparator比较元素
于 2023-10-12 21:16:41 首次发布
博客介绍了在Java中使用Comparable接口对同学成绩进行降序排序,成绩相同时按年龄从小到大排序。还阐述了用Comparator实现按姓名排序,以及Comparable和Comparator的混合使用,指出Comparator优先级高于Comparable。
155

被折叠的 条评论
为什么被折叠?



