灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2229回复:0

Java朝花夕拾の实现Comparable接口

楼主#
更多 发布于:2012-09-08 09:36

java.lang.Comparable接口中唯一的方法是compareTo(),在该方法中可以进行简单的相等比较以及执行顺序比较,接口实现框架如下:
[java]
public class ComparableImpl implements Comparable<ComparableImpl> {

    @Override
    public int compareTo(ComparableImpl o) {
        // TODO Auto-generated method stub  
        return 0;
    }

}
public class ComparableImpl implements Comparable<ComparableImpl> {
@Override
public int compareTo(ComparableImpl o) {
  // TODO Auto-generated method stub
  return 0;
}
}
一个类实现了Comparable接口,则说明它的实例具有内在的排序关系,就可以跟多种泛型算法以及依赖于该接口的集合实现进行协作。依赖于比较关系的类包括有序集合类TreeSet和TreeMap,以及工具类Collections和Arrays。
若一个数组中的元素实现了Comparable接口,则可以直接使用Arrays类的sort方法对这个数组进行排序。java平台库中的所有值类(value classes)都实现了Comparable接口。
Comparable的规范说明如下:将当前这个对象与指定对象进行顺序比较。当该对象小于、等于或大于指定对象时,分别返回一个负整数、零或者正整数。如果由于指定对象的类型而使得无法进行比较,则抛出ClassCastException异常。www.atcpu.com
compareTo方法的实现必须满足如下几个限制条件:自反性、对称性、传递性和非空性。
一般来说,comparaTo方法的相等测试应该返回与equals方法相同的结果。如果相同,则由compareTo方法施加的顺序关系被称为“与equals一致”;如果不同,则顺序关系被称为“与equals不一致”。如果一个类的compareTo方法与equals方法的顺序关系不一致,那么它仍然能正常工作,只是,如果一个有序集合包含了该类的实例,则这个集合可能无法遵循某些集合接口的通用约定。因为集合接口的通用约定是按照equals方法定义的,而有序集合使用了由compareTo施加的相等测试。下面是实现了Comparable接口的类,同时,该类还重写了equals和hashCode等方法:
[java]
public abstract class ZLTextPosition implements Comparable<ZLTextPosition> {
    
    public abstract int getParagraphIndex();
    public abstract int getElementIndex();
    public abstract int getCharIndex();
    
    public boolean samePositionAs(ZLTextPosition position) {
        return
            getParagraphIndex() == position.getParagraphIndex() ;;
            getElementIndex() == position.getElementIndex() ;;
            getCharIndex() == position.getCharIndex();
    }
    
    @Override
    public int compareTo(ZLTextPosition position) {
        final int p0 = getParagraphIndex();
        final int p1 = position.getParagraphIndex();
        if (p0 != p1) {
            return p0 < p1 ? -1 : 1;
        }
        
        final int e0 = getElementIndex();
        final int e1 = position.getElementIndex();
        if (e0 != e1) {
            return e0 < e1 ? -1 : 1;
        }
        
        final int c0 = getCharIndex();
        final int c1 = position.getCharIndex();
        if (c0 != c1) {
            return c0 < c1 ? -1 : 1;
        }
        return 0;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof ZLTextPosition)) {
            return false;
        }
        
        final ZLTextPosition position = (ZLTextPosition)obj;
        return samePositionAs(position);
    }
    
    @Override
    public int hashCode() {
        return (getParagraphIndex() << 16) + (getElementIndex() << 8) + getCharIndex();
    }
    
    @Override
    public String toString() {
        return getClass().getName() + " " + getParagraphIndex() + " " + getElementIndex() + " " + getCharIndex();
    }
    
}
public abstract class ZLTextPosition implements Comparable<ZLTextPosition> {

public abstract int getParagraphIndex();
public abstract int getElementIndex();
public abstract int getCharIndex();

public boolean samePositionAs(ZLTextPosition position) {
  return
   getParagraphIndex() == position.getParagraphIndex() ;;
   getElementIndex() == position.getElementIndex() ;;
   getCharIndex() == position.getCharIndex();
}

@Override
public int compareTo(ZLTextPosition position) {
  final int p0 = getParagraphIndex();
  final int p1 = position.getParagraphIndex();
  if (p0 != p1) {
   return p0 < p1 ? -1 : 1;
  }
  
  final int e0 = getElementIndex();
  final int e1 = position.getElementIndex();
  if (e0 != e1) {
   return e0 < e1 ? -1 : 1;
  }
  
  final int c0 = getCharIndex();
  final int c1 = position.getCharIndex();
  if (c0 != c1) {
   return c0 < c1 ? -1 : 1;
  }
  return 0;
}

@Override
public boolean equals(Object obj) {
  if (this == obj) {
   return true;
  }
  if (!(obj instanceof ZLTextPosition)) {
   return false;
  }
  
  final ZLTextPosition position = (ZLTextPosition)obj;
  return samePositionAs(position);
}

@Override
public int hashCode() {
  return (getParagraphIndex() << 16) + (getElementIndex() << 8) + getCharIndex();
}

@Override
public String toString() {
  return getClass().getName() + " " + getParagraphIndex() + " " + getElementIndex() + " " + getCharIndex();
}

}



喜欢0 评分0
游客

返回顶部