学习 准备 尝试 谨慎小心

0%

【转载】Java中比较器的实现方法

比较器的使用场景

  • 判断对象集合中是否包含某对象
  • 排序对象集合

当然可以直接通过“==”来判断两个对象的地址是否相同,地址相同那么对象肯定就相同了。但在实际情况中,通常根据对象的属性进行比较

从最新的JDK8来看,有三种实现对象比较的方法:

  1. 复写Object类的equals()方法

  2. 继承Comparable接口,实现compareTo()方法

  3. 定义一个对象比较器,继承Comparator接口,实现compare()方法

复写equals()方法

复写 equals() 方法, 一般用于自己实现对象数组排序的情况,而对于要使用java内置的排序算法时,使用后面两种方式都是可行的。

ps:复写 equals()方法怎么实现对象数组排序啊? equals()只能实现比较,不能排序吧。

实现Comparable接口

实现 compareTo() 方法

1
public class Employee implements Comparable<Employee>{
2
    private Integer id;
3
    private String name;
4
    private Integer age;
5
    private Double salary;
6
7
    public Employee() {
8
    }
9
10
    public Employee(Integer id, String name, Integer age, Double salary) {
11
        this.id = id;
12
        this.name = name;
13
        this.age = age;
14
        this.salary = salary;
15
    }
16
17
    @Override
18
    public String toString() {
19
        return "Employee{" +
20
                "id=" + id +
21
                ", name='" + name + '\'' +
22
                ", age=" + age +
23
                ", salary=" + salary +
24
                '}';
25
    }
26
27
    @Override
28
    public int compareTo(Employee o) {
29
        if (this.salary > o.salary) {
30
            // 由高到底排列
31
            return -1;
32
        } else if(this.salary < o.salary) {
33
            return 1;
34
        } else {
35
            if (this.age > o.age) {
36
                // 由低到高排列
37
                return 1;
38
            } else if(this.age < o.age) {
39
                return -1;
40
            } else {
41
                return 0;
42
            }
43
        }
44
    }
45
    // getter 和 setter
46
    
47
    public static void main(String[] args) {
48
        Employee[] emps = {
49
                new Employee(5, "田七", 20, 5000D),
50
                new Employee(4, "赵六", 46, 18000D),
51
                new Employee(3, "王五", 35, 15000D),
52
                new Employee(1, "张三", 24, 5000D),
53
                new Employee(2, "李四", 36, 15000D)
54
        };
55
        java.util.Arrays.sort(emps);
56
        for(Employee e : emps) {
57
            System.out.println(e);
58
        }
59
    }
60
}

Employee实现了Comparable接口的compareTo()方法,将员工先按照薪资salary从高到底排列,薪资相同时,按照年龄age从低到高排列。

输出结果:

1
Employee{id=4, name='赵六', age=46, salary=18000.0}
2
Employee{id=3, name='王五', age=35, salary=15000.0}
3
Employee{id=2, name='李四', age=36, salary=15000.0}
4
Employee{id=5, name='田七', age=20, salary=5000.0}
5
Employee{id=1, name='张三', age=24, salary=5000.0}

比较器Comparator对象

定义一个对象比较器类,继承Comparator接口,实现compare()方法

一般我们使用以上两种方法就能够满足实际的开发问题。但是当出现以下情况时,就需要用到Comparator接口:

要在已经开发好的代码基础上,完善对象的比较功能,又不想更改之前的代码,这种情况下,从JDK1.8之后出现了Comparator接口,是对这种情况的一个弥补。

1
public class Student {
2
    private Integer id;
3
    private String name;
4
    private Double score;
5
6
    public Student() {
7
    }
8
9
    public Student(Integer id, String name, Double score) {
10
        this.id = id;
11
        this.name = name;
12
        this.score = score;
13
    }
14
15
    @Override
16
    public String toString() {
17
        return "Employee{" +
18
                "id=" + id +
19
                ", name='" + name + '\'' +
20
                ", score=" + score +
21
                '}';
22
    }
23
24
    // getter 和 setter
25
26
    public static void main(String[] args) {
27
        Student[] sts = new Student[]{
28
                new Student(1, "小戴",60D),
29
                new Student(3,"小王",90D),
30
                new Student(2,"老王",80D),
31
                new Student(4, "小萱",95D)
32
        };
33
34
        Comparator<Student> studentComparator = new Comparator<Student>() {
35
            @Override
36
            public int compare(Student o1,Student o2) {
37
                if(o1.getScore() > o2.getScore()){
38
                    // 由小到大排列
39
                    return 1;
40
                }else if(o1.getScore() < o2.getScore()){
41
                    return -1;
42
                }else{
43
                    return 0;
44
                }
45
            }
46
        };
47
        java.util.Arrays.sort(sts, studentComparator);
48
49
        for(Student s : sts) {
50
            System.out.println(s);
51
        }
52
    }
53
}