Comparable和Comparator是Java核心API提供的两个接口。从它们的名字,我们大致可以猜到它们用来以某种方式比较一些事物。但它们到底是什么,它们之间有又哪些差别呢?下面的两个例子回答了这个问题。这个例子用来比较HDTV的大小。阅读完下面的代码,对于如何使用Comparable和Comparator会很清楚。
1.Comparable
一个类为了能比较自身对象与其他对象实现这个接口。一个类如果要比较自己的实例就必须实现这个接口。而且要实现compareTo()这个方法。示例:
?
class HDTV implements Comparable{ private int size; private String brand; public HDTV(int size, String brand) { this.size = size; this.brand = brand; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } @Override public int compareTo(HDTV tv) { if (this.getSize() > tv.getSize()) return 1; else if (this.getSize() < tv.getSize()) return -1; else return 0; } } public class Main { public static void main(String[] args) { HDTV tv1 = new HDTV(55, "Samsung"); HDTV tv2 = new HDTV(60, "Sony"); if (tv1.compareTo(tv2) > 0) { System.out.println(tv1.getBrand() + " is better."); } else { System.out.println(tv2.getBrand() + " is better."); } }
?
2.Comparator
在一些情景下,你可能不想改变一个类使其变成可比较的。诸如这样的案例,如果你想根据特定的属性/字段来比较对象的话,可以使用Comparator。比如,我们可以根据身高或者年龄等来比较两个人。 我们需要实现compare()方法。现在让我们以另一种方式来比较TV的大小。Comparator最常见的一个应用就是排序。Collections和Arrays都提供了根据Comparator排序的方法。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class HDTV {
private int size;
private String brand;
public HDTV(int size, String brand) {
this.size = size;
this.brand = brand;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
class SizeComparator implements Comparator
{
@Override
public int compare(HDTV tv1, HDTV tv2) {
int tv1Size = tv1.getSize();
int tv2Size = tv2.getSize();
if (tv1Size > tv2Size) {
return 1;
} else if (tv1Size < tv2Size) {
return -1;
} else {
return 0;
}
}
}
public class Main {
public static void main(String[] args) {
HDTV tv1 = new HDTV(55, "Samsung");
HDTV tv2 = new HDTV(60, "Sony");
HDTV tv3 = new HDTV(42, "Panasonic");
ArrayList
al = new ArrayList
(); al.add(tv1); al.add(tv2); al.add(tv3); Collections.sort(al, new SizeComparator()); for (HDTV a : al) { System.out.println(a.getBrand()); } } }
另外,我们还可以使用Collections.reverseOrder()来获取一个反序比较器。类似于下面:
?
ArrayListal = new ArrayList (); al.add(3); al.add(1); al.add(2); System.out.println(al); Collections.sort(al); System.out.println(al); Comparator comparator = Collections.reverseOrder(); Collections.sort(al,comparator); System.out.println(al);
?
3.如何选择?
简单来说,一个类实现Comparable接口后会变成比较的,也就是说它的实例之间可以互相比较。
一个类实现了Comparator接口主要用于两种情况:1)可以传递给一个排序方法,像Collections.sort()或者Arrays.sort(),精确地控制排序方式。2)它还可以用来控制特定数据结构的排序,比如TreeSet或者TreeMap等。
例如,创建一个TreeSet,我们或者向其构造器传递一个comparator或者实现comparable接口。
实现1(使用comparator)
?
class Dog {
int size;
Dog(int s) {
size = s;
}
}
class SizeComparator implements Comparator
{
@Override
public int compare(Dog d1, Dog d2) {
return d1.size - d2.size;
}
}
public class ImpComparable {
public static void main(String[] args) {
TreeSet
d = new TreeSet
(new SizeComparator()); // pass comparator d.add(new Dog(1)); d.add(new Dog(2)); d.add(new Dog(1)); } }
?
实现2(使用comparable)
?
class Dog implements Comparable{ int size; Dog(int s) { size = s; } @Override public int compareTo(Dog o) { return o.size - this.size; } } public class ImpComparable {