Java 中的 Comparable 与 Comparator\

java programming java8object oriented programming

Comparable

当我们想要使用对象的一个​​属性来比较对象时,可以使用 Comparable 接口。它被认为是对象的自然排序。考虑一个员工列表,我们希望按姓名排序作为默认排序顺序。Comparable 接口具有目标类必须实现的 compareTo() 方法。

class Employee implements Comparable {
   String name;
   public int compareTo(Employee o) {        
      return name.compareTo(o.name);
   }
}

现在,可以使用 Collections.sort() 方法按名称对上述对象的集合进行排序。

Collections.sort(employees);

Comparator

Comparator 接口提供了一个 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;        
      }    
   }
}

示例

在此示例中,我们展示了如何使用可比较接口和比较器接口分别根据员工姓名、ID 和年龄对员工列表进行排序。

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("无序列表");
      System.out.println(employees);

      //按员工姓名的自然顺序排序,
      //由 compareTo 方法提供
      Collections.sort(employees);

      //有序列表
      System.out.println("按姓名排序列表");
      System.out.println(employees);

      //按员工 ID 排序,
      //由 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 "
[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 ]]

相关文章