你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

007—java (Comparable vs comparator)详细推导

2021/10/23 17:42:46

jdk内置的比较对象的接口     comparable和comparator两个接口用于指定比较的规则

对象的比较前提:要有比较的条件

Comparable:内部比较器(实现在需要比较的类的内部
如何使用内部比较器:
    1.让比较对象所在的类实现 Comparable接口

    2.实现/重写接口内的抽象方法
          public int compareTo(Object o) {}
         比较两个对象只有一个形参 需要借助于this
    3.完成比较规则的制定
         >0: 前一个对象>后一个对象
         ==0: 前一个对象=后一个对象
         <0: 前一个对象<后一个对象

外部比较器Comparable:

public class Person implements Comparable{
    String name;
    int age;
    double salary;

    public Person(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

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

    @Override
    public int compareTo(Object o) {
        Person person = (Person)o;

//        return this.age - person.age;

        return Double.compare(this.salary, person.salary);
    }
}
public class Test {
    public static void main(String[] args) {
        int m = 10;
        int n = 20;
        Person p1 = new Person("张三", 20, 9990);
        Person p2 = new Person("安琪拉", 19, 9999);

        int i = p1.compareTo(p2);
        System.out.println(i);


    }
}

 比较方法的抽取过程:

public static void sort(Cat[] cats) {
        for (int i = 0; i < cats.length - 1; i++) {

            for (int j = 0; j < cats.length - 1 - i; j++) {

                // 如果前一个对象>后一个对象 调用比较规则比较
                if(cats[j].compareTo(cats[j+1])>0){

                    Cat temp = cats[j];
                    cats[j]=cats[j+1];
                    cats[j+1]=temp;
                }
            }
        }
    }

 

public class ArraysUtils {
    //可以比较所有引用类型的方法

    public static void sort(Comparable[] comm){
        for (int i = 0; i < comm.length - 1; i++) {
            for (int j = 0; j < comm.length - 1 -i; j++) {
                if (comm[j].compareTo(comm[j+1])>0){
                    Comparable temp = comm[j];
                    comm[j] = comm[j + 1];
                    comm[j + 1] = temp;
                }
            }
        }
    }
}

Cloneable接口 可以实现对象的复制

 如何实现对对象的克隆

                 1.实现接口 Cloneable

                 2.重写方法 Object 的 clone()

                 3.对象调用clone() 方法 即可完成当前对象的克隆

public class Person implements Cloneable{
    String name;
    int age;

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

    public Person() {
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person p1 = new Person("李白", 20);

        System.out.println("p1 = " + p1);

       /* Person p2 = p1;
        p2.name = "杜甫";

        System.out.println("p2 = " + p2);
        System.out.println("p1 = " + p1);*/

        Object clone = p1.clone();
        Person p2 = (Person)clone;
        p2.name = "杜甫";

        System.out.println("clone = " + clone);

        System.out.println("p2 = " + p2);


    }
}
 @Override

protected Object clone() throws CloneNotSupportedException {
    return super.clone();
 }

Object类中的 clone()方法     权限修饰符为protected ,即不同包下的子类可见,但是子类对象不可见。

外部比较器Comparator:

内部比较器需要实现的比较条件在比较器的外部。(不需要污染当前类)

public class Person {
    String name;
    int age;
    double salary;

    public Person(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

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

   /* @Override
    public int compare(Object o1, Object o2) {
        Person p1 = (Person)o1;
        Person p2 = (Person)o2;
        return p1.age - p2.age;

    }*/
}
public class Test {
    public static void main(String[] args) {
        Person p1 = new Person("张三", 20, 30000);
        Person p2 = new Person("李四", 30, 20000);
        Person p3 = new Person("王五", 10, 20000);
        Person p4 = new Person("赵六", 80, 20000);

        Person persons [] =new Person[]{p1,p2,p3,p4};
        System.out.println("比较前");

        for (Person person : persons) {
            System.out.println(person);
        }


        /*SortByAge sortByAge = new SortByAge();
        int compare = sortByAge.compare(p1, p2);
        System.out.println("compare = " + compare);*/

        Arrays.sort(persons, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                Person p1 = (Person)o1;
                Person p2 = (Person)o2;
                return p1.age - p2.age;

            }
        });
        for (Person person : persons) {
            System.out.println("person = " + person);
        }


    }
}