您的位置:首页 > 编程语言 > Java开发

Java 中 Comparable 和 Comparator 比较

2017-08-13 00:51 573 查看
Comparable 简介

Comparable 是排序接口。

若一个类实现了Comparable接口,就意味着"该类支持排序"。 既然实现Comparable接口的类支持排序,假设现在存在"实现Comparable接口的类的对象的List列表(或数组)",则该List列表(或数组)可以通过Collections.sort(或 Arrays.sort)进行排序。

此外,"实现Comparable接口的类的对象"可以用作"有序映射(如TreeMap)"中的键或"有序集合(TreeSet)"中的元素,而不需要指定比较器。

Comparable 定义

Comparable 接口仅仅只包括一个函数,它的定义如下:

public interface Comparable<T> {
/**
* 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.
*
* <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
* -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
* implies that <tt>x.compareTo(y)</tt> must throw an exception iff
* <tt>y.compareTo(x)</tt> throws an exception.)
*
* <p>The implementor must also ensure that the relation is transitive:
* <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies
* <tt>x.compareTo(z)>0</tt>.
*
* <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
* implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
* all <tt>z</tt>.
*
* <p>It is strongly recommended, but <i>not</i> strictly required that
* <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>.  Generally speaking, any
* class that implements the <tt>Comparable</tt> interface and violates
* this condition should clearly indicate this fact.  The recommended
* language is "Note: this class has a natural ordering that is
* inconsistent with equals."
*
* <p>In the foregoing description, the notation
* <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
* <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
* <tt>0</tt>, or <tt>1</tt> according to whether the value of
* <i>expression</i> is negative, zero or positive.
*
* @param   o the object to be compared.
* @return  a negative integer, zero, or a positive integer as this object
*          is less than, equal to, or greater than the specified object.
*
* @throws NullPointerException if the specified object is null
* @throws ClassCastException if the specified object's type prevents it
*         from being compared to this object.
*/
public int compareTo(T o);
}


说明:
假设我们通过 x.compareTo(y) 来"比较x和y的大小"。若返回"负数",意味着"x比y小(x-y<0)";返回"零",意味着"x等于y(x-y=0)";返回"正数",意味着"x大于y(x-y>0)"。

Comparator 简介

Comparator 是比较器接口。

我们若需要控制某个类的次序,而该类本身不支持排序(即没有实现Comparable接口);那么,我们可以建立一个"该类的比较器"来进行排序。这个"比较器"只需要实现Comparator接口即可。

也就是说,我们可以通过"实现Comparator类来新建一个比较器",然后通过该比较器对类进行排序。

Comparator 定义

Comparator 接口包括两个函数,它的定义如下:

package java.util;

public interface Comparator<T> {

int compare(T o1, T o2);

boolean equals(Object obj);
}


说明:
(01) 若一个类要实现Comparator接口:它一定要实现compareTo(T o1, T o2) 函数,但可以不实现 equals(Object obj) 函数。

为什么可以不实现 equals(Object obj) 函数呢? 因为任何类,默认都是已经实现了equals(Object obj)的。 Java中的一切类都是继承于java.lang.Object,在Object.java中实现了equals(Object obj)函数;所以,其它所有的类也相当于都实现了该函数。

(02) int compare(T o1, T o2) 是"比较o1和o2的大小"。返回"负数",意味着"o1比o2小(o1-o2<0)";返回"零",意味着"o1等于o2(o1-o2=0)";返回"正数",意味着"o1大于o2(o1-o2>0)"。

Comparator 和 Comparable 比较

Comparable是排序接口;若一个类实现了Comparable接口,就意味着"该类支持排序"。
而Comparator是比较器;我们若需要控制某个类的次序,可以建立一个"该类的比较器"来进行排序。

我们不难发现:Comparable相当于"内部比较器"(自身就可以排序,调用Collections.sort不需要指定比较器),而Comparator相当于"外部比较器"。

我们通过一个测试程序来对这两个接口进行说明。源码如下:

package test;

import java.util.*;
import java.lang.Comparable;

/**
* @desc "Comparator"和“Comparable”的比较程序。
* (01) "Comparable"
* 它是一个排序接口,只包含一个函数compareTo()。
* 一个类实现了Comparable接口,就意味着“该类本身支持排序”,它可以直接通过Arrays.sort() 或 Collections.sort()进行排序。
* (02) "Comparator"
* 它是一个比较器接口,包括两个函数:compare() 和 equals()。
* 一个类实现了Comparator接口,那么它就是一个“比较器”。其它的类,可以根据该比较器去排序。
* <p>
* 综上所述:Comparable是内部比较器,而Comparator是外部比较器。
* 一个类本身实现了Comparable比较器,就意味着它本身支持排序;若它本身没实现Comparable,也可以通过外部比较器Comparator进行排序。
*/
public class CompareComparatorAndComparableTest {

public static void main(String[] args) {
// 新建ArrayList(动态数组)
ArrayList<Person> list = new ArrayList<Person>();
// 添加对象到ArrayList中
list.add(new Person("ccc", 20));
list.add(new Person("AAA", 30));
list.add(new Person("bbb", 10));
list.add(new Person("ddd", 40));

// 打印list的原始序列
System.out.printf("Original  sort, list:%s\n", list);

// 对list进行排序
// 这里会根据“Person实现的Comparable<String>接口”进行排序,即会根据“name”进行排序
        Collections.sort(list);
System.out.printf("Name      sort, list:%s\n", list);

// 通过“比较器(AscAgeComparator)”,对list进行排序
// AscAgeComparator的排序方式是:根据“age”的升序排序
Collections.sort(list, new AscAgeComparator());
System.out.printf("Asc(age)  sort, list:%s\n", list);

// 通过“比较器(DescAgeComparator)”,对list进行排序
// DescAgeComparator的排序方式是:根据“age”的降序排序
  Collections.sort(list, new DescAgeComparator());
System.out.printf("Desc(age) sort, list:%s\n", list);

// 判断两个person是否相等
testEquals();
}

/**
* @desc 测试两个Person比较是否相等。
* 由于Person实现了equals()函数:若两person的age、name都相等,则认为这两个person相等。
* 所以,这里的p1和p2相等。
* <p>
* TODO:若去掉Person中的equals()函数,则p1不等于p2
*/
private static void testEquals() {
Person p1 = new Person("eee", 100);
Person p2 = new Person("eee", 100);
if (p1.equals(p2)) {
System.out.printf("%s EQUAL %s\n", p1, p2);
} else {
System.out.printf("%s NOT EQUAL %s\n", p1, p2);
}
}

/**
* @desc Person类。
* Person实现了Comparable接口,这意味着Person本身支持排序
*/
private static class Person implements Comparable<Person> {
int age;
String name;

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

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String toString() {
return name + " - " + age;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Person person = (Person) o;

if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;

}

@Override
public int hashCode() {
int result = age;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}

/**
* @desc 实现 “Comparable<String>” 的接口,即重写compareTo<T t>函数。
* 这里是通过“person的名字”进行比较的
*/
@Override
public int compareTo(Person person) {
return name.compareTo(person.name);
}
}

/**
* @desc AscAgeComparator比较器
* 它是“Person的age的升序比较器”
*/
private static class AscAgeComparator implements Comparator<Person> {

@Override
public int compare(Person p1, Person p2) {
return p1.getAge() - p2.getAge();
}
}

/**
* @desc DescAgeComparator比较器
* 它是“Person的age的升序比较器”
*/
private static class DescAgeComparator implements Comparator<Person> {

@Override
public int compare(Person p1, Person p2) {
return p2.getAge() - p1.getAge();
}
}

}


下面对这个程序进行说明

a) Person类定义。如下:

/**
* @desc Person类。
* Person实现了Comparable接口,这意味着Person本身支持排序
*/
private static class Person implements Comparable<Person> {
int age;
String name;

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

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String toString() {
return name + " - " + age;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Person person = (Person) o;

if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;

}

@Override
public int hashCode() {
int result = age;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}

/**
* @desc 实现 “Comparable<String>” 的接口,即重写compareTo<T t>函数。
* 这里是通过“person的名字”进行比较的
*/
@Override
public int compareTo(Person person) {
return name.compareTo(person.name);
}
}


说明:
(01) Person类代表一个人,Persong类中有两个属性:age 和 name。
(02) Person类实现了Comparable接口,因此它能被排序

b) 在main()中,我们创建了Person的List数组(list)。如下:

// 新建ArrayList(动态数组)
ArrayList<Person> list = new ArrayList<Person>();
// 添加对象到ArrayList中
list.add(new Person("ccc", 20));
list.add(new Person("AAA", 30));
list.add(new Person("bbb", 10));
list.add(new Person("ddd", 40));


c) 接着,我们打印出list的全部元素。如下:

// 打印list的原始序列
System.out.printf("Original sort, list:%s\n", list);


d) 然后,我们通过Collections的sort()函数,对list进行排序。

由于Person实现了Comparable接口,因此通过sort()排序时,会根据Person支持的排序方式,即 compareTo(Person person) 所定义的规则进行排序。如下:

// 对list进行排序
// 这里会根据“Person实现的Comparable<String>接口”进行排序,即会根据“name”进行排序
Collections.sort(list);
System.out.printf("Name sort, list:%s\n", list);


e) 对比Comparable和Comparator

我们定义了两个比较器 AscAgeComparatorDescAgeComparator,来分别对Person进行 升序降低 排序。

e.1) AscAgeComparator比较器

它是将Person按照age进行升序排序。代码如下:

/**
* @desc AscAgeComparator比较器
*       它是“Person的age的升序比较器”
*/
private static class AscAgeComparator implements Comparator<Person> {

@Override
public int compare(Person p1, Person p2) {
return p1.getAge() - p2.getAge();
}
}


e.2) DescAgeComparator比较器

它是将Person按照age进行降序排序。代码如下:

/**
* @desc DescAgeComparator比较器
*       它是“Person的age的升序比较器”
*/
private static class DescAgeComparator implements Comparator<Person> {

@Override
public int compare(Person p1, Person p2) {
return p2.getAge() - p1.getAge();
}
}


f) 运行结果
运行程序,输出如下:

Original  sort, list:[ccc - 20, AAA - 30, bbb - 10, ddd - 40]
Name      sort, list:[AAA - 30, bbb - 10, ccc - 20, ddd - 40]
Asc(age)  sort, list:[bbb - 10, ccc - 20, AAA - 30, ddd - 40]
Desc(age) sort, list:[ddd - 40, AAA - 30, ccc - 20, bbb - 10]
eee - 100 EQUAL eee - 100
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: