看着JDK做出了基础测试题目,用这两种方式都实现了,却不知道他们到底有什么区别,很多的类都实现了comparable接口,而comparator实现类很少,就这区别吗?望大神解答。谢谢。。。下面是代码:
实现comparable接口的:
- package com.itheima;
- import java.util.Iterator;
- import java.util.TreeSet;
- public class Test10 {
- public static void main(String[] args) {
- //声明创建TreeSet集合泛型为Student
- TreeSet<Student> set = new TreeSet<Student>();
- //声明创建五个student并加入set集合
- Student stu1 = new Student("小明", 23, 98.5);
- Student stu2 = new Student("小红", 24, 45.6);
- Student stu3 = new Student("小强", 25, 84.2);
- Student stu4 = new Student("小刚", 26, 84.2);
- Student stu5 = new Student("小松", 23, 98);
- set.add(stu1);
- set.add(stu2);
- set.add(stu3);
- set.add(stu4);
- set.add(stu5);
- System.out.println("set大小:" + set.size());
- //调用所定义的对set集合的输出函数
- Myout(set);
- }
- public static void Myout(TreeSet<Student> set) {
- //迭代set集合
- Iterator<Student> it = set.iterator();
- //取出迭代器中的元素
- while (it.hasNext()) {
- Student stu = (Student) it.next();
- System.out.println(stu.toString());
- }
- }
- }
- /*
- * 定义Student类,并继承Comparable并实现compareto()方法
- * 在此我直接将泛型加入,省去compareto()中的强转
- *
- */
- class Student implements Comparable<Student>{
- private String name;
- private int age;
- private double score;
- public Student() {
- }
- //增加带参数构造器方便添加数据
- public Student(String name, int age, double score) {
- super();
- this.name = name;
- this.age = age;
- this.score = score;
- }
- //增加get set 方法
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public double getScore() {
- return score;
- }
- public void setScore(double score) {
- this.score = score;
- }
- //覆盖compareTo()方法,定义出自己所需的情况
- @Override
- public int compareTo(Student o) {
- Student stu = o;
- //下面几个if else 语句实现了先通过分数进行排序和年龄进行排序
- //如果两者都相同则通过姓名进行排序。
- //返回的-1、1和0也可以是其它的证书或者负数
- if (this.score < stu.score)
- return (int) -1;
- else if (this.score > stu.score)
- return 1;
- else if (this.age > stu.age)
- return 1;
- else if (this.age < stu.age)
- return -1;
- else if (this.score == stu.score && this.age == stu.age)
- //分数年龄相同则返回姓名的字典顺序差。
- return this.name.compareTo(stu.name);
- return 0;
- }
- //覆盖toString()方法方便调试和显示
- @Override
- public String toString() {
- return "Student [age=" + age + ", name=" + name + ", score=" + score
- + "]";
- }
- }
复制代码 实现comparator接口的
- package com.itheima;
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.TreeSet;
- public class Test10 {
- /**
- * 10、声明类Student,包含3个成员变量:name、age、score,创建5个对象装入TreeSet,
- * 按照成绩排序输出结果(考虑成绩相同的问题)。
- */
- public static void main(String[] args) {
- //声明创建TreeSet集合泛型为Student
- TreeSet<Student> set = new TreeSet<Student>(new myStudentComparactor());
- //声明创建五个student并加入set集合
- Student stu1 = new Student("小明", 23, 98.5);
- Student stu2 = new Student("小红", 24, 45.6);
- Student stu3 = new Student("小强", 25, 84.2);
- Student stu4 = new Student("小刚", 26, 84.2);
- Student stu5 = new Student("小松", 23, 98);
- set.add(stu1);
- set.add(stu2);
- set.add(stu3);
- set.add(stu4);
- set.add(stu5);
- System.out.println("set大小:" + set.size());
- //调用所定义的对set集合的输出函数
- Myout(set);
- }
- public static void Myout(TreeSet<Student> set) {
- //迭代set集合
- Iterator<Student> it = set.iterator();
- //取出迭代器中的元素
- while (it.hasNext()) {
- Student stu = (Student) it.next();
- System.out.println(stu.toString());
- }
- }
- }
- class Student {
- private String name;
- private int age;
- private double score;
- public Student() {
- }
- //增加带参数构造器方便添加数据
- public Student(String name, int age, double score) {
- super();
- this.name = name;
- this.age = age;
- this.score = score;
- }
- //增加get set 方法
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public double getScore() {
- return score;
- }
- public void setScore(double score) {
- this.score = score;
- }
-
-
- //覆盖toString()方法方便调试和显示
- @Override
- public String toString() {
- return "Student [age=" + age + ", name=" + name + ", score=" + score
- + "]";
- }
- }
- //自定义排序器
- class myStudentComparactor implements Comparator<Student>{
- @Override
- public int compare(Student o1, Student o2) {
- if (o1.getScore() > o2.getScore())
- return (int) -1;
- else if (o1.getScore() < o2.getScore())
- return 1;
- else if (o1.getAge() > o2.getAge())
- return 1;
- else if (o1.getAge() < o2.getAge())
- return -1;
- else if (o1.getScore() == o2.getScore() && o1.getAge() == o2.getAge())
- return o1.getName().compareTo(o2.getName());
- return 0;
- }
-
- }
复制代码
|
|