简介

  • 在Java的开发中,会经常用到Java对象的排序,但是一些自然排序不能满足我们的需要,所以我们就需要定制化排序
  • 而Comparable和Comparator都是可以完成对Java对象的排序,二者并不同
  • Comparable 是可比较的意思,其表达的是赋予对象能够比较的能力。若一个类实现了Comparable接口,就意味着”该类支持排序”。实现Comparable接口的类的对象可以用作有序映射(如TreeMap)中的键或有序集合(TreeSet)中的元素,而不需要指定比较器
  • Comparator 则是比较器。我们若需要控制某个类的次序,而该类本身不支持排序(即没有实现Comparable接口),那么,我们可以建立一个”该类的比较器”来进行排序。这个”比较器”只需要实现Comparator接口即可。也就是说,我们可以通过“实现Comparator类来新建一个比较器,然后通过该比较器对类进行排序
  • Comparable相当于”内部比较器”,而Comparator相当于”外部比较器”

    Comparable使用

    ```java @Data @AllArgsConstructor @NoArgsConstructor public class UserInfo implements Comparable { private int index; private int age; private String username; private String address;

    @Override public int compareTo(UserInfo o) {

    1. // 排序规则
    2. // 1.先根据 index 升序
    3. // 2.如果 index 相等,在根据 age 升序排序
    4. if (this.index > o.index) {
    5. return 1;
    6. } else if (this.index < o.index) {
    7. return -1;
    8. }
    9. if (this.age > o.age) {
    10. return 1;
    11. } else if (this.age < o.age) {
    12. return -1;
    13. }
    14. return 0;

    } }

```java
public class ComparableTest {
    private static final List<UserInfo> USER_INFO_LIST = new ArrayList<>();
    static {
        USER_INFO_LIST.add(new UserInfo(1, 2, "icanci-1-2", "icanci-1-2"));
        USER_INFO_LIST.add(new UserInfo(1, 3, "icanci-1-3", "icanci-1-3"));
        USER_INFO_LIST.add(new UserInfo(3, 2, "icanci-1-2", "icanci-1-2"));
        USER_INFO_LIST.add(new UserInfo(11, 2, "icanci-1-2", "icanci-1-2"));
        USER_INFO_LIST.add(new UserInfo(112, 2, "icanci-1-2", "icanci-1-2"));
        USER_INFO_LIST.add(new UserInfo(12, 1, "icanci-1-2", "icanci-1-2"));
    }

    public static void main(String[] args) {
        Collections.sort(USER_INFO_LIST);
        System.out.println(USER_INFO_LIST);
    }
}

Comparator使用

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AdminInfo {
    private int    index;
    private int    age;
    private String username;
    private String address;
}
public class UserInfoComparator implements Comparator<AdminInfo> {
    @Override
    public int compare(AdminInfo o1, AdminInfo o2) {
        // 排序规则
        // 1.先根据 index 升序
        // 2.如果 index 相等,在根据 age 升序排序
        if (o1.getIndex() > o2.getIndex()) {
            return 1;
        } else if (o1.getIndex() < o2.getIndex()) {
            return -1;
        }
        if (o1.getAge() > o2.getAge()) {
            return 1;
        } else if (o1.getAge() < o2.getAge()) {
            return -1;
        }
        return 0;
    }
}
public class ComparatorTest {
    private static final List<AdminInfo> ADMIN_INFO_LIST = new ArrayList<>();
    static {
        ADMIN_INFO_LIST.add(new AdminInfo(1, 2, "icanci-1-2", "icanci-1-2"));
        ADMIN_INFO_LIST.add(new AdminInfo(1, 3, "icanci-1-3", "icanci-1-3"));
        ADMIN_INFO_LIST.add(new AdminInfo(3, 2, "icanci-1-2", "icanci-1-2"));
        ADMIN_INFO_LIST.add(new AdminInfo(11, 2, "icanci-1-2", "icanci-1-2"));
        ADMIN_INFO_LIST.add(new AdminInfo(112, 2, "icanci-1-2", "icanci-1-2"));
        ADMIN_INFO_LIST.add(new AdminInfo(12, 1, "icanci-1-2", "icanci-1-2"));
    }

    public static void main(String[] args) {
        Collections.sort(ADMIN_INFO_LIST, new UserInfoComparator());
        System.out.println(ADMIN_INFO_LIST);
    }
}