区别

Comparable 是在集合内部定义的方法实现的排序,只能有一种比较方式。在 lang 包下。
Comparator 是在集合外部实现的排序,使用策略模式,可以自定义多种比较方式。在 util 包下。

Comparable

定义

package java.lang;
import java.util.*;

public interface Comparable<T> {
    public int compareTo(T o);
}

用法

package com.zmz.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 张明泽
 * Create by 2022/9/2 15:12
 */
public class ComparableTest {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person(19,"xiaoming"));
        list.add(new Person(26,"xiaomeng"));
        list.add(new Person(18,"xiaozhang"));
        Collections.sort(list);
        System.out.println(list);
        // [Person{age=18, name='xiaozhang'}, Person{age=19, name='xiaoming'}, Person{age=26, name='xiaomeng'}]
    }
}
class Person implements Comparable{
    int age;
    String name;
    
    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }
    
    @Override
    public int compareTo(Object o) {
        if (o instanceof Person) {
            Person person = (Person) o;
            return this.age - person.age;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

Comparator

定义

用 Comparator 是 策略模式(strategy design pattern),就是不改变对象自身,而用一个策略对象(strategy object)来改变它的行为。

package java.util;

public interface Comparator<T> {

    int compare(T o1, T o2);

    boolean equals(Object obj);
}

用法

package com.zmz.collection;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author 张明泽
 * Create by 2022/9/2 15:33
 */
public class ComparatorTest {
    public static void main(String[] args) {
        List<Teacher> list = new ArrayList<>();
        list.add(new Teacher(19,"xiaoming"));
        list.add(new Teacher(26,"xiaomeng"));
        list.add(new Teacher(18,"xiaozhang"));
        list.sort(new AscTeacher());
        System.out.println(list);
        // [Teacher{age=18, name='xiaozhang'}, Teacher{age=19, name='xiaoming'}, Teacher{age=26, name='xiaomeng'}]
        
        list.sort(new DescTeacher());
        System.out.println(list);
        // [Teacher{age=26, name='xiaomeng'}, Teacher{age=19, name='xiaoming'}, Teacher{age=18, name='xiaozhang'}]
        // Collections.sort(list,new DescTeacher());
        // 若 list 中为数字可以利用Lambda表达式简写
        // Arrays.sort(arr, (a,b) -> a[0] - b[0]);
    }
}
class Teacher {
    int age;
    String name;

    public Teacher(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
class AscTeacher implements Comparator<Teacher> {
    @Override
    public int compare(Teacher o1, Teacher o2) {
        return o1.age - o2.age;
    }
}
class DescTeacher implements Comparator<Teacher> {
    @Override
    public int compare(Teacher o1, Teacher o2) {
        return o2.age - o1.age;
    }
}
最后修改:2022 年 09 月 02 日 03 : 55 PM
赏杯咖啡喝 谢谢您~