Comparable和Comparator的区别以及Lambda表达式实现

Comparable和Comparator的区别

1.comparable里面的ComparaTo()是TreeSet和TreeMap里面add()与put()方法里面调用对象的ComparaTo(),通过返回值,来确定它,是否存在or顺序。他是内置的,所以如果自定义对象用TreeSet来存的话,得重写ComparaTo.这里不能,用Lambda表达式,Lambda表达式和匿名内部类一样,就是为了创建对象,而这里不用。

2.Comparator是人工比较器,它的优先级高于Comparable,当系统检测到里面实现了Comparator,它会优先于Comparable使用,它是脱离你所存的对象的,它通过创建自己的对象,定义自己的规则,它的compare方法,方法有两个参数T o1和T o2,是泛型的表示方式,分别表示待比较的两个对象,方法返回值和Comparable接口一样是int。然后集合调用自己的构造,传进去,在使用add()或put()方法时,自动调用Compare方法。

实例代码1


package com.chd.day06;



import java.util.Comparator;

import java.util.Set;

import java.util.TreeSet;



public class Lambda02 {



    static int compararr(String s1, String s2) {

        return (s1.length() - s2.length()) == 0 ? s1.compareTo(s2) : (s1.length() - s2.length());

    }



    public static void main(String[] args) {

        // TODO Auto-generated method stub

        // 开始

//         ImplementsComparator im = new ImplementsComparator();

        /*匿名内部类

        Comparator im =new Comparator() {



            @Override

            public int compare(Object o1, Object o2) {

                // TODO Auto-generated method stub

                // 向下转型

                String s1 = (String) o1;

                String s2 = (String) o2;

                // 先按照长度比

                int num = s1.length() - s2.length();

                // 长度相同,再按照字典顺序比

                return num == 0 ? s1.compareTo(s2) : num;

            }

            

        };

        */

        // 第一次Lambda:

        /*

         * Comparator im = (s1,s2)->{ String o1 = (String)s1; String o2 = (String)s2;

         * int num = o1.length()-o2.length(); return num==0?o1.compareTo(o2):num; };

         */ 

         //第二次Lambda: 

        

        Comparator im =(s1,s2)->(s1.length()-s2.length())==0?s1.compareTo(s2):(s1.length()-s2.length());

         

        // 第三次Lambda;

//        Comparator im =Lambda02::compararr;

        Set set = new TreeSet<>(im);



        set.add("skdj");

        set.add("skdj");

        set.add("skdj");

        set.add("skdj");



        System.out.println(set);



    }



}



class ImplementsComparator implements Comparator {



//    @Override

//    public int compare(T o1, T o2) {

//        // 比较字符串的长度

//        if (!(o1 instanceof String)) {

//            throw new ClassCastException("类型转换错误");

//        }

//        if (!(o2 instanceof String)) {

//            throw new ClassCastException("类型转换错误");

//        }

//        // 向下转型

//        String s1 = (String) o1;

//        String s2 = (String) o2;

//        // 先按照长度比

//        int num = s1.length() - s2.length();

//        // 长度相同,再按照字典顺序比

//        return num == 0 ? s1.compareTo(s2) : num;

//    }



    @Override

    public int compare(String s1, String s2) {

        // TODO Auto-generated method stub

        return (s1.length()-s2.length())==0?s1.compareTo(s2):(s1.length()-s2.length());

    }



}

实例代码2


package com.chd.day06;



import java.util.Set;

import java.util.TreeSet;



public class Lambda01 {



    public static void main(String[] args) {

        // TODO Auto-generated method stub



        Set set = new TreeSet<>();

        set.add(new Person("张三",23));

        set.add(new Person("张三",23));

        set.add(new Person("张三",24));

        set.add(new Person("李四",24));

        System.out.println(set);

    }



}

//匿名内部类:是为了创建一个实例:而这里需要的是重写这个方法

//class Person implements Comparable{

//    public String name;

//    public int age;

//    public Person() {}

//    public Person(String name,int age) {

//        super();

//        this.name=name;this.age=age;

//        

//    }

//    

//    @Override

//    public String toString() {

//        return "Person [name=" + name + ", age=" + age + "]";

//    }

//    Comparable mi = new Comparable() {

//

//        @Override

//        public int compareTo(Person person) {

//            // TODO Auto-generated method stub

//            int num = 

//            return num==0?this.age-person.age:num;

//        }

//        

//    };

    

//}

//Lambda表达式01

class Person {

    public String name;

    public int age;

    public Person() {}

    public Person(String name,int age) {

        super();

        this.name=name;this.age=age;

        

    }

    

    @Override

    public String toString() {

        return "Person [name=" + name + ", age=" + age + "]";

    }



    Comparable person=(p)->{    

        int num = this.name.compareTo(p.name);

        return num==0?this.age-p.age:num;

    };

    

    

}



/*加泛型

class Person implements Comparable{

    public String name;

    public int age;

    public Person() {}

    public Person(String name,int age) {

        super();

        this.name=name;this.age=age;

        

    }

    

    @Override

    public String toString() {

        return "Person [name=" + name + ", age=" + age + "]";

    }

    @Override

    public int compareTo( Person person) {    

        int num = this.name.compareTo(person.name);

        return num==0?this.age-person.age:num;

    }

    

}

*/



/*未加泛型

class Person implements Comparable{

    public String name;

    public int age;

    public Person() {}

    public Person(String name,int age) {

        super();

        this.name=name;this.age=age;

        

    }

    

    @Override

    public String toString() {

        return "Person [name=" + name + ", age=" + age + "]";

    }

    @Override

    public int compareTo( Object o) {

        if(!(o instanceof Person)) {

            throw new ClassCastException("类型转换错误");

        }

        Person person = (Person) o;

        

        int num = this.name.compareTo(person.name);

        return num==0?this.age-person.age:num;

    }

    

}

*/





你可能感兴趣的:(疑惑问题)