当我们要使用对象的属性之一比较对象时,使用可比较接口。它被视为对象的自然排序。考虑一个雇员列表,我们希望按姓名将其排序为默认排序顺序。可比接口具有compareTo()
目标类必须实现的方法。
class Employee implements Comparable { String name; public int compareTo(Employee o) { return name.compareTo(o.name); } }
现在,可以使用Collections.sort()方法按名称对上述对象的集合进行排序。
Collections.sort(employees);
比较器接口提供了compare()
一种比较同一类的两个对象的方法。在这里它提供了灵活性,因为我们可以为要比较的对象的每个属性创建n个比较器。请请看以下示例:
class IdComparator implements Comparator<Employee> { public int compare(Employee o1, Employee o2) { if (o1.getId() < o2.getId()) { return -1; }else if (o1.getId() > o2.getId()) { return 1; } else { return 0; } } } class AgeComparator implements Comparator<Employee> { public int compare(Employee o1, Employee o2) { if (o1.getAge() < o2.getAge()) { return -1; }else if (o1.getAge() > o2.getAge()) { return 1; } else { return 0; } } }
在此示例中,我们展示了使用可比较以及比较器接口分别根据员工的姓名,身份证和年龄对员工列表进行排序的用法。
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Tester { public static void main(String[] args) { List<Employee> employees = new ArrayList<>(); employees.add(new Employee(1, 20, "A")); employees.add(new Employee(4, 26, "D")); employees.add(new Employee(2, 23, "C")); employees.add(new Employee(3, 25, "B")); employees.add(new Employee(5, 24, "E")); //无序列表 System.out.println("Unsorted List"); System.out.println(employees); //按员工姓名的自然顺序排序 //由compareTo方法提供 Collections.sort(employees); //订购列表 System.out.println("Sorted List on Name"); System.out.println(employees); //按员工编号排序 //由IdComparator提供 Collections.sort(employees, new IdComparator()); //订购列表 System.out.println("Sorted List on Id"); System.out.println(employees); //按员工年龄排序 //由AgeComparator提供 Collections.sort(employees, new AgeComparator()); //订购列表 System.out.println("Sorted List on Age"); System.out.println(employees); } } class Employee implements Comparable<Employee> { private int id; private int age; private String name; public Employee(int id, int age, String name) { this.id = id; this.age = age; this.name = name; } public int compareTo(Employee o) { return name.compareTo(o.name); } public String toString() { return "\n[Id: " + id + ", age: " + age + ", name: " + name +" ]"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class IdComparator implements Comparator<Employee> { public int compare(Employee o1, Employee o2) { if (o1.getId() < o2.getId()) { return -1; }else if (o1.getId() > o2.getId()) { return 1; } else { return 0; } } } class AgeComparator implements Comparator<Employee> { public int compare(Employee o1, Employee o2) { if (o1.getAge() < o2.getAge()) { return -1; }else if (o1.getAge() > o2.getAge()) { return 1; } else { return 0; } } }
输出结果
Unsorted List [ [Id: 1, age: 20, name: A ], [Id: 4, age: 26, name: D ], [Id: 2, age: 23, name: C ], [Id: 3, age: 25, name: B ], [Id: 5, age: 24, name: E ]] Sorted List on Name [ [Id: 1, age: 20, name: A ], [Id: 3, age: 25, name: B ], [Id: 2, age: 23, name: C ], [Id: 4, age: 26, name: D ], [Id: 5, age: 24, name: E ]] Sorted List on Id [ [Id: 1, age: 20, name: A ], [Id: 2, age: 23, name: C ], [Id: 3, age: 25, name: B ], [Id: 4, age: 26, name: D ], [Id: 5, age: 24, name: E ]] Sorted List on Age [ [Id: 1, age: 20, name: A ], [Id: 2, age: 23, name: C ], [Id: 5, age: 24, name: E ], [Id: 3, age: 25, name: B ], [Id: 4, age: 26, name: D ]]