Java 中Comparable和Comparator区别比较

来源:互联网 发布:淘宝飞猪机票 编辑:程序博客网 时间:2024/05/29 10:12

Comparable 是排序接口:

          若一个类实现了Comparable接口,就意味着“该类支持排序”。  即然实现Comparable接口的类支持排序,假设现在存在“实现Comparable接口的类的对象的List列表(或数组)”,则该List列表(或数组)可以通过 Collections.sort(或 Arrays.sort)进行排序。
         此外,“实现Comparable接口的类的对象”可以用作“有序映射(如TreeMap)”中的键或“有序集合(TreeSet)”中的元素,而不需要指定比较器。例如TreeSet<Person> set = new TreeSet<Person>(new Compara());其中Compara是实现Comparable接口的类。

         此接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它的自然比较方法 。实现此接口的对象列表(和数组)可以通过 Collections.sort (和 Arrays.sort)进行自动排序。实现此接口的对象可以用作有序映射表中的键或有序集合中的元素,无需指定比较器。 强烈推荐(虽然不是必需的)使自然排序与 equals 一致。所谓与equals一致是指对于类C 的每一个 e1 e2 来说,当且仅当 (e1.compareTo((Object)e2) == 0)e1.equals((Object)e2) 具有相同的布尔值时,类 C 的自然排序才叫做与 equals 一致

     强烈推荐 (x.compareTo(y)==0) == (x.equals(y)) 这种做法,但不是 严格要求这样做。一般来说,任何实现 Comparable 接口和违背此条件的类都应该清楚地指出这一事实。注释应当加上:“注意:此类具有与 equals 不一致的自然排序。

Comparator 是比较器接口:

         我们若需要控制某个类的次序,而该类本身不支持排序(即没有实现Comparable接口);那么,我们可以建立一个“该类的比较器”来进行排序。这个“比较器”只需要实现Comparator接口即可。
         也就是说,我们可以通过“实现Comparator类来新建一个比较器”,然后通过该比较器对类进行排序。

说明:
(1) 若一个类要实现Comparator接口:它一定要实现compareTo(T o1, T o2) 函数,但可以不实现 equals(Object obj) 函数。为什么可以不实现 equals(Object obj) 函数呢? 因为任何类,默认都是已经实现了equals(Object obj)的。 Java中的一切类都是继承于java.lang.Object,在Object.java中实现了equals(Object obj)函数;所以,其它所有的类也相当于都实现了该函数。
(2) int compare(T o1, T o2) 是“比较o1和o2的大小”。返回“负数”,意味着“o1比o2小”;返回“零”,意味着“o1等于o2”;返回“正数”,意味着“o1大于o2”。


Comparator 和 Comparable 比较

1. Comparable是排序接口:若一个类实现了Comparable接口,就意味着“该类支持排序”。
2. Comparator是比较器:我们若需要控制某个类的次序,可以建立一个“该类的比较器”来进行排序。
3. Comparable相当于“内部比较器”,而Comparator相当于“外部比较器”。

例子: comparator

public class Person {    int id;    String name;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

public class SortComparator {    class PersonCompara implements Comparator<Person> {        @Override        public int compare(Person o1, Person o2) {            if (o1.getId() == o2.getId()) {                return 0;            } else if (o1.getId() > o2.getId()) {                return 1;            } else {                return -1;            }        }    }    public static void main(String[] args) {        List<Person> personList = new ArrayList<Person>();        Person person1 = new Person();        person1.setId(1);        person1.setName("name1");        personList.add(person1);        Person person2 = new Person();        person2.setId(2);        person2.setName("name2");        personList.add(person2);        Person person3 = new Person();        person3.setId(3);        person3.setName("name3");        personList.add(person3);        Collections.sort(personList, new SortComparator().new PersonCompara());    }}

例子二:compareable

public class PersonWithCompare implements Comparable<PersonWithCompare> {    int id;    String name;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public int compareTo(PersonWithCompare o) {        if (id == o.getId()) {            return 0;        } else if (id > o.getId()) {            return 1;        } else {            return -1;        }    }}

public class SortComparable {    public static void main(String[] args) {        List<PersonWithCompare> personList = new ArrayList<PersonWithCompare>();        PersonWithCompare person1 = new PersonWithCompare();        person1.setId(1);        person1.setName("name1");        personList.add(person1);        PersonWithCompare person2 = new PersonWithCompare();        person2.setId(2);        person2.setName("name2");        personList.add(person2);        PersonWithCompare person3 = new PersonWithCompare();        person3.setId(3);        person3.setName("name3");        personList.add(person3);        Collections.sort(personList);    }}


参考链接:http://www.jb51.net/article/41730.htm

0 0
原创粉丝点击