1.Comparable
Comparable是被类实现的一个借口为了能够对象本身和其他对象的比较,类本身必须实现此接口,以便能够比较它的实例。要求实现的方法是compareTo()。下面是一个例子:
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个男人可以根据`name`或`age`等等来比较(这个 Comparable就不能实现了)
要求实现的方法是compare()。现在,让我们用另一种方式按大小来比较这些电视。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<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()); } } } Output: Panasonic Samsung Sony
通常我们可能会使用Collections.reverseOrder()方法来获得一个降序序列。如下所示:
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的类将会是对其他类是一个Comparator。1)它可以被传递到一个排序的方法,如Collections.sort()或Arrays.sort()中,为了精确地控制排序顺序;2)也可用于控制特定的数据结构,如Set或Map的排序。
例如,要创建一个TreeSet。我们可以通过构造一Comparator或者实现Comparable。
方法一:TreeSet(Comparator comparator)
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()); // pass comparator d.add(new Dog(1)); d.add(new Dog(2)); d.add(new Dog(1)); } }
方法二:实现Comparable
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)); } }相关文章:
1.Comparable
2.Comparator
Comparable vs Comparator in Java
Java中的Comparable与Comparator的区别,布布扣,bubuko.com
Java中的Comparable与Comparator的区别
原文:http://blog.****.net/sunling_sz/article/details/21232405