Java教程:实现Comparable接口


Java.lang.Comparable接口中唯一的方法是compareTo(),在该方法中可以进行简单的相等比较以及执行顺序比较,接口实现框架如下:

[java]

  1. public class ComparableImpl implements Comparable<ComparableImpl> {  
  2.   
  3.     @Override  
  4.     public int compareTo(ComparableImpl o) {  
  5.         // TODO Auto-generated method stub   
  6.         return 0;  
  7.     }  
  8.   
  9. }  

一个类实现了Comparable接口,则说明它的实例具有内在的排序关系,就可以跟多种泛型算法以及依赖于该接口的集合实现进行协作。依赖于比较关系的类包括有序集合类TreeSet和TreeMap,以及工具类Collections和Arrays。

若一个数组中的元素实现了Comparable接口,则可以直接使用Arrays类的sort方法对这个数组进行排序。Java平台库中的所有值类(value classes)都实现了Comparable接口。

Comparable的规范说明如下:将当前这个对象与指定对象进行顺序比较。当该对象小于、等于或大于指定对象时,分别返回一个负整数、零或者正整数。如果由于指定对象的类型而使得无法进行比较,则抛出ClassCastException异常。

compareTo方法的实现必须满足如下几个限制条件:自反性、对称性、传递性和非空性。

一般来说,comparaTo方法的相等测试应该返回与equals方法相同的结果。如果相同,则由compareTo方法施加的顺序关系被称为“与equals一致”;如果不同,则顺序关系被称为“与equals不一致”。如果一个类的compareTo方法与equals方法的顺序关系不一致,那么它仍然能正常工作,只是,如果一个有序集合包含了该类的实例,则这个集合可能无法遵循某些集合接口的通用约定。因为集合接口的通用约定是按照equals方法定义的,而有序集合使用了由compareTo施加的相等测试。下面是实现了Comparable接口的类,同时,该类还重写了equals和hashCode等方法:

[java]
  1. public abstract class ZLTextPosition implements Comparable<ZLTextPosition> {  
  2.       
  3.     public abstract int getParagraphIndex();  
  4.     public abstract int getElementIndex();  
  5.     public abstract int getCharIndex();  
  6.       
  7.     public boolean samePositionAs(ZLTextPosition position) {  
  8.         return  
  9.             getParagraphIndex() == position.getParagraphIndex() &&  
  10.             getElementIndex() == position.getElementIndex() &&  
  11.             getCharIndex() == position.getCharIndex();  
  12.     }  
  13.       
  14.     @Override  
  15.     public int compareTo(ZLTextPosition position) {  
  16.         final int p0 = getParagraphIndex();  
  17.         final int p1 = position.getParagraphIndex();  
  18.         if (p0 != p1) {  
  19.             return p0 < p1 ? -1 : 1;  
  20.         }  
  21.           
  22.         final int e0 = getElementIndex();  
  23.         final int e1 = position.getElementIndex();  
  24.         if (e0 != e1) {  
  25.             return e0 < e1 ? -1 : 1;  
  26.         }  
  27.           
  28.         final int c0 = getCharIndex();  
  29.         final int c1 = position.getCharIndex();  
  30.         if (c0 != c1) {  
  31.             return c0 < c1 ? -1 : 1;  
  32.         }  
  33.         return 0;  
  34.     }  
  35.       
  36.     @Override  
  37.     public boolean equals(Object obj) {  
  38.         if (this == obj) {  
  39.             return true;  
  40.         }  
  41.         if (!(obj instanceof ZLTextPosition)) {  
  42.             return false;  
  43.         }  
  44.           
  45.         final ZLTextPosition position = (ZLTextPosition)obj;  
  46.         return samePositionAs(position);  
  47.     }  
  48.       
  49.     @Override  
  50.     public int hashCode() {  
  51.         return (getParagraphIndex() << 16) + (getElementIndex() << 8) + getCharIndex();  
  52.     }  
  53.       
  54.     @Override  
  55.     public String toString() {  
  56.         return getClass().getName() + " " + getParagraphIndex() + " " + getElementIndex() + " " + getCharIndex();  
  57.     }  
  58.       
  59. }  

相关内容