Comparable.compareTo的返回值在Java中意味着什么? [英] What do the return values of Comparable.compareTo mean in Java?

查看:380
本文介绍了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 Version

In short:

this.compareTo(that)

返回


  • 负数如果这个<那

  • 0 如果这= =那

  • 一个正整数如果这个>那个

  • a negative int if this < that
  • 0 if this == that
  • a positive int if this > that

此方法的实现确定< <的实际语义code>> == (我的意思是 == 在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:

  • the Object Ordering section of the Collection Trail in the Sun Java Tutorial
  • Effective Java by Joshua Bloch, especially item 12: Consider implementing Comparable
  • Java Generics and Collections by Maurice 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中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆