gpt4 book ai didi

java - 实现比较方法的规则

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:34:21 24 4
gpt4 key购买 nike

像compareTo,必须是“自反,反对称,传递”,有什么规则可以实现compare方法吗??谢谢

最佳答案

来自 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."

关于java - 实现比较方法的规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14732551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com