+
+
+
+
+
+
Comparable 和 Comparator 是JAVA 核心API 提供的两个接口。从它们的名字我们可以得知它们是用来比较东西的在某种意义上说。但他们到底是什么,它们之间的区别是什么呢?以下是回答这个问题的2个例子。比较两个HDTV的尺寸简单的例子。在阅读代码后,如何使用Comparable vs. Comparator就比较容易了。
+
1.Comparable
一个类实现Comparable接口,以便能够比较对象本身与一些其他对象。类本身必须实现此接口以便能够比较其实例(的)。所需实现的方法compareto()。这里就是一个例子:
+
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| class HDTV implements Comparable<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; } @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."); } } }
|
+
+Sony is better.
+
+
2.Comparator
在某些情况下,您可能不希望更改一个类,并使其具有可比较性。在这种情况下,如果你想比较基于某些属性/字段的对象,可以使用Comparator。例如,2个人可以基础上的身高或年龄等进行比较(Comparable不能被用于这种比较)。
+
需要实现的方法是compare()。现在让我们用另一种方式来比较这些电视的大小。比较器的一个常见用途是排序。集合和数组类提供了一种使用比较器的排序方法。
+
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| 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<HDTV> { @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<HDTV> al = new ArrayList<HDTV>(); al.add(tv1); al.add(tv2); al.add(tv3); Collections.sort(al, new SizeComparator()); for (HDTV a : al) { System.out.println(a.getBrand()); } } }
|
+
+Panasonic
Samsung
Sony
+
+
我们经常会使用Collections.reverseorder()方法得到一个降序比较器。如下:
+
1 2 3 4 5 6 7 8 9 10 11
| ArrayList<Integer> al = new ArrayList<Integer>(); al.add(3); al.add(1); al.add(2); System.out.println(al); Collections.sort(al); System.out.println(al); Comparator<Integer> comparator = Collections.reverseOrder(); Collections.sort(al,comparator); System.out.println(al);
|
+
Output:
+
+[3,1,2]
[1,2,3]
[3,2,1]
+
+
3.什么时候使用?
简单地说,一个类实现Comparable可以被比性,这意味着它的实例之间可以相互比较。
一个类实现Comparator将主要在两种情况下使用:1)它可以通过一个排序方法,如Collections.sort()或Arrays.sort(),允许精确控制的排序顺序和2)它也可以被用来控制的顺序一定的数据结构,如有序集合(例如TreeSet)或排序图(例如,Treemap)。
+
例如,创建一个TreeSet。我们可以通过构造一个Comparator或使对象类可比较。
+
方法1——TreeSet(Comparator comparator)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Dog { int size; Dog(int s) { size = s; } } class SizeComparator implements Comparator<Dog> { @Override public int compare(Dog d1, Dog d2) { return d1.size - d2.size; } } public class ImpComparable { public static void main(String[] args) { TreeSet<Dog> d = new TreeSet<Dog>(new SizeComparator()); d.add(new Dog(1)); d.add(new Dog(2)); d.add(new Dog(1)); } }
|
+
方法2——实现Comparable接口
+
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Dog implements Comparable<Dog>{ int size; Dog(int s) { size = s; } @Override public int compareTo(Dog o) { return o.size - this.size; } } public class ImpComparable { public static void main(String[] args) { TreeSet<Dog> d = new TreeSet<Dog>(); d.add(new Dog(1)); d.add(new Dog(2)); d.add(new Dog(1)); } }
|
+
原文链接
+
+
+