本文介绍了Comparable.compareTo的返回值在Java中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 返回 0 ,返回 1 并返回 -1 中的/ code> Java中的 compareTo() ?What is the difference between returning 0, returning 1 and returning -1 in compareTo() in Java?推荐答案 官方定义 来自 Comparable.compareTo(T): 将此对象与订单的指定对象。返回负整数,零或正整数,因为此对象小于等于或大于指定对象。 Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.对于所有x和y,实现者必须确保 sgn(x.compareTo(y))== -sgn(y.compareTo(x))。 (这意味着x.compareTo(y)必须抛出异常iff y.compareTo(x)抛出异常。)The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)实现者还必须确保关系是可传递的:(x.compareTo(y)> 0& y.compareTo(z)> 0)表示x.compareTo(z )> 0。The implementor must also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.最后,实现者必须确保 x.compareTo(y)== 0意味着 sgn(x.compareTo) (z))== sgn(y.compareTo(z)),对于所有z。Finally, the implementor must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.强烈建议,但不是严格要求(x.compareTo(y)== 0)==(x.equals(y))。 一般来说,任何的类都会实现Comparable接口,如果清楚地表明这一事实,则违反了这个条件。 推荐的语言是注意:这个类的自然顺序是与equals不一致。It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."在上述描述,表示法sgn(表达式)指定数学符号函数,定义为返回-1, 0或1中的一个,具体取决于值表达式为负数,零或为正。In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive. 我的版本 简而言之:My VersionIn short:this.compareTo(that)返回 负数如果这个<那 0 如果这= =那 一个正整数如果这个>那个 a negative int if this < that0 if this == thata positive int if this > that此方法的实现确定< > 和 == (我的意思是 == 在java的对象标识运算符意义上)where the implementation of this method determines the actual semantics of < > and == (I don't mean == in the sense of java's object identity operator)"abc".compareTo("def")将产生小于0的值 abc 按字母顺序排在 def 之前。will yield something smaller than 0 as abc is alphabetically before def.Integer.valueOf(2).compareTo(Integer.valueOf(1))会产生一些东西大于0,因为2大于1. will yield something larger than 0 because 2 is larger than 1. 注意:对于实现Comparable的类来说,在javadocs中声明它的compareTo()方法的语义是一种好习惯。Note: It is good practice for a class that implements Comparable to declare the semantics of it's compareTo() method in the javadocs. 注意: ÿ你应该至少阅读下列之一:Note: you should read at least one of the following: 对象订购部分 Sun Trail中的Collection Trail 教程 有效Java Joshua Bloch,特别是项目12: 考虑实施可比较 Java Generics and Collections by Maurice Naftalin,Philip Wadler,第3.1章:可比较 the Object Ordering section ofthe Collection Trail in the Sun JavaTutorialEffective Java byJoshua Bloch, especially item 12:Consider implementing ComparableJava Generics and Collections byMaurice Naftalin, Philip Wadler, chapter 3.1: Comparable 警告:你永远不应该依赖compareTo的返回值 -1 , 0 和 1 。你应该总是测试 x< 0 , x == 0 , x> 0 ,分别为。Warning: you should never rely on the return values of compareTo being -1, 0 and 1. You should always test for x < 0, x == 0, x > 0, respectively. 这篇关于Comparable.compareTo的返回值在Java中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-27 11:31