• Comparable和Comparator的差别


    原文地址:http://leihuang.org/2014/11/16/Comparable-Vs-Comparator/


    Comparable和Comparator都是用来实现集合中元素的比較、排序的,仅仅是Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序,所以。如想实现排序,就须要在集合外定义 Comparator 接口的方法或在集合内实现 Comparable 接口的方法。

    Comparator位于包java.util下,而Comparable位于包 java.lang下.

    Comparable 是一个对象本身就已经支持自比較所须要实现的接口(如 String、Integer 自己就能够完毕比較大小操作,已经实现了Comparable接口)

    自己定义的类要在增加list容器中后可以排序,可以实现Comparable接口,在用Collections类的sort方法排序时,假设不指定Comparator,那么就以自然顺序排序,如API所说:

    Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface

    这里的自然顺序就是实现Comparable接口设定的排序方式。

    而 Comparator 是一个专用的比較器,当这个对象不支持自比較或者自比較函数不能满足你的要求时。你能够写一个比較器来完毕两个对象之间大小的比較。

    能够说一个是自已完毕比較,一个是外部程序实现比較的区别而已。

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

    Comparable interface

    比如我们定义一个类Country,我们希望他的对象有大小关系,且根据countryId来排序的。所以我们让Country实现Comparable接口,使其具备大小关系的能力,代码例如以下:

    class Country implements Comparable{
        private int countryId ;
        @Override
        public int compareTo(Object o) {
            Country country = (Country)o ;
            return this.getCountryId()>country.getCountryId()?1:this.getCountryId()==
                    country.getCountryId()?0:-1 ;
        }
    }


    此时Country就具备了比較的能力了,如同Integer,String类一样,此时Country对象的集合就能够通过Collections.sort(),或者Arrays.sort(),来进行排序了。

    以下我们来看看Comparator使怎样实现的。

    Comparator interface

    就像我们之前说的一样,Comparator事实上就好像一个工具,它不是使得Country具有排序性。而是他不改变Country类本身,而是单独实现一个排序工具。来提供给Country用。以下我们就来实现这个排序工具类。

    class Country{
        private int id ;
        public Country(int id){
            this.id = id ;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
    }
    class CountryByIdComparator implements Comparator<Country>{
        @Override
        public int compare(Country o1, Country o2) {
            return o1.getId()>o2.getId()?1:o1.getId()==o2.getId()?0:-1 ;
        }
    }


    大家注意到没。单单看Country类。事实上并没有跟比較有不论什么关系,可是它却能通过实现Comparator借口的工具类ContryByIdComparator来实现自己主动排序。此时调用的Collections.sort()就须要两个參数了,一个是集合,一个是实现Comparator接口的工具类。

    我们也能够利用匿名内部类来实现,而不须要单独定义ContryByIdComparator这样一个工具类,代码例如以下:

    Collections.sort(cityList,new Comparator<City>(){
                @Override
                public int compare(City o1, City o2) {
                    return o1.getId()>o2.getId()?1:o1.getId()==o2.getId()?0:-1 ;
                }
    
            }) ;


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

    以下这张图就是它们两的差别:

    image

    Refference

    1. Comparable与Comparator的差别
    2. Difference between Comparator and Comparable in Java

    2014-11-16 17:27:25

    Brave,Happy,Thanksgiving !

  • 相关阅读:
    Java 插入排序
    Java 浮点型与双精度数值比较
    Java 包装类Integer的值比较
    ORA-00942 table or view does not exist
    logging模块
    面向对象
    模块和包
    异常处理
    序列化模块
    css3选择器
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6877949.html
Copyright © 2020-2023  润新知