美文网首页
Comparable 和 Comparator

Comparable 和 Comparator

作者: 一个追寻者的故事 | 来源:发表于2020-04-07 18:13 被阅读0次

    Comparable和 Comparator是两个接口,从字面上看这两个接口都是用来做比较用的,但是jdk里面不可能定义两个功能相同的接口,所以他们肯定有不同的用处。

    一、Comparable

    1.1说明

    Comparable可以认为是一个内比较器,实现了Comparable接口的类有一个特点,就是这些 类是可以和自己比较的,至于具体和另一个实现了Comparable接口的类如何比较,则依赖compareTo方法的实现。

    public class Boy implements Comparable<Boy>{
        private String name;
        private int age;
    
        public Boy(int age, String name){
            this.age = age;
            this.name = name;
        }
    
        /**
          Compares this object with the specified object for order.  Returns a
         * negative integer, zero, or a positive integer as this object is less
         * than, equal to, or greater than the specified object.
        */
        @Override
        public int compareTo(Boy o) {
            return this.age - o.age;
        }
    
        @NonNull
        @Override
        public String toString() {
            return "Age: " + age + ", Name: " + name;
        }
    }
    
    

    测试:

        Boy boy = new Boy(12, "Jone");
        Boy boy1 = new Boy(22, "Jonson");
        Boy boy2 = new Boy(18, "Jack");
    
        List boys = new ArrayList();
        boys.add(boy);
        boys.add(boy1);
        boys.add(boy2);
    
        for (int i = 0; i < boys.size(); i++) {
            System.out.println(" " + boys.get(i));
        }
    
        System.out.println("=========================");
    
        //排序
        Collections.sort(boys);
        //bubbleSort(boys);
    
        for (int i = 0; i < boys.size(); i++) {
            System.out.println(" " + boys.get(i));
        }
    

    结果:

     Age: 12, Name: Jone
     Age: 22, Name: Jonson
     Age: 18, Name: Jack
    =========================
     Age: 12, Name: Jone
     Age: 18, Name: Jack
     Age: 22, Name: Jonson
    

    也可以自己手写排序算法,比如冒泡:

    private <T extends Comparable<? super  T>> void bubbleSort(List<T> list){
            for (int i = list.size() - 1; i > 0; i--) {
    
                boolean flag = true;
                for (int j = 0; j < i; j++) {
    
                    T obj = list.get(j);
                    T obj2 = list.get(j + 1);
    
                    if (obj.compareTo(obj2) > 0){
                        T tmp = obj;
                        list.set(j, obj2);
                        list.set(j + 1, tmp);
                        flag = false;
                    }
                }
    
                if (flag){
                    break;
                }
            }
        }
    

    二、Comparator

    2.1说明

    Comparator可以认为是是一个外比较器,有两种情况可以使用实现Comparator接口的方式:
    1、 一个对象不支持自己和自己比较(没有实现Comparable接口),但是又想对两个对象进行比较。
    2、 一个对象实现了Comparable接口,但是开发者认为compareTo方法中的比较方式并不是自己想要的那种比较方式。
    Comparator接口里面有一个compare方法,方法有两个参数T o1和T o2,是泛型的表示方式,分别表示待比较的两个对象

    public class Girl {
        private String name;
        private int age;
    
        public Girl(int age, String name){
            this.age = age;
            this.name = name;
        }
    
        @NonNull
        @Override
        public String toString() {
            return "Age: " + age + ", Name: " + name;
        }
    
        public int getAge(){
            return age;
        }
    }
    
    //创建比较器
    public class GrilComparator implements Comparator<Girl> {
        @Override
        public int compare(Girl o1, Girl o2) {
            return o1.getAge() - o2.getAge();
        }
    }
    

    测试:

        Girl girl = new Girl(2, "Lucy");
        Girl girl1 = new Girl(22, "LiLi");
        Girl girl2 = new Girl(8, "Anne");
    
        List girls = new ArrayList();
        girls.add(girl);
        girls.add(girl1);
        girls.add(girl2);
    
        for (int i = 0; i < girls.size(); i++) {
            System.out.println(" " + girls.get(i));
        }
    
        System.out.println("=========================");
    
        GrilComparator comparator = new GrilComparator();
        Collections.sort(girls, comparator);
        //insertSort(girls, comparator);
        
        for (int i = 0; i < girls.size(); i++) {
            System.out.println(" " + girls.get(i));
        }
    

    结果:

     Age: 2, Name: Lucy
     Age: 22, Name: LiLi
     Age: 8, Name: Anne
    =========================
     Age: 2, Name: Lucy
     Age: 8, Name: Anne
     Age: 22, Name: LiLi
    
    

    也可以自己手写排序算法,比如选择排序:

    private <T> void insertSort(List<T> list, Comparator<? super T> comparator) {
        for (int i = 1; i < list.size(); i++) {
    
            T target = list.get(i);
    
            for (int j = i; j > 0; j--) {
                T obj = list.get(j);
                T obj2 = list.get(j - 1);
                int res = comparator.compare(obj, obj2);
                if(res < 0){
                    list.set(j, obj2);
                }else{
                    list.set(j, target);
                    break;
                }
            }
        }
    }
    

    参考:https://blog.csdn.net/wlh2015/java/article/details/83959462

    相关文章

      网友评论

          本文标题:Comparable 和 Comparator

          本文链接:https://www.haomeiwen.com/subject/maonphtx.html