加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 实现比较方法的规则

发布时间:2020-12-15 05:00:05 所属栏目:Java 来源:网络整理
导读:像compareTo,必须是“反身,反对称和传递”,是否有任何规则来实现比较方法? 谢谢 解决方法 从 Comparator API开始: The implementor must ensure that sgn(compare(x,y)) == -sgn(compare(y,x)) for all x and y. (This implies that compare(x,y) must thr
像compareTo,必须是“反身,反对称和传递”,是否有任何规则来实现比较方法?
谢谢

解决方法

从 Comparator API开始:

  1. The implementor must ensure that sgn(compare(x,y)) == -sgn(compare(y,x)) for all x and y. (This implies that compare(x,y) must throw an exception if and only if compare(y,x) throws an
    exception.)
  2. The implementor must also ensure that the relation is transitive: ((compare(x,y)>0) && (compare(y,z)>0)) implies compare(x,z)>0.
  3. Finally,the implementor must ensure that compare(x,y)==0 implies that sgn(compare(x,z))==sgn(compare(y,z)) for all z.
  4. It is generally the case,but not strictly required that (compare(x,y)==0) == (x.equals(y)). Generally speaking,any comparator that violates this condition should clearly indicate this fact. The recommended language is “Note: this comparator imposes orderings that are inconsistent with equals.”

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读