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.
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.)
The implementor must also ensure that
the relation is transitive:
(x.compareTo(y)>0 && y.compareTo(z)>0)
implies x.compareTo(z)>0.
Finally, the implementor must ensure
that x.compareTo(y)==0 implies that
sgn(x.compareTo(z)) ==
sgn(y.compareTo(z)), for all z.
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."
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)
returns
a negative int if this < that
0 if this == that
a positive int if this > that
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)
Examples
"abc".compareTo("def")
will yield something smaller than 0 as abc is alphabetically before def.
Integer.valueOf(2).compareTo(Integer.valueOf(1))
will yield something larger than 0 because 2 is larger than 1.
Some additional points
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
发布评论
评论(7)
官方定义
来自 的参考文档Comparable.compareTo(T):
我的版本
简而言之:
返回
了
<
>
和==
的实际语义(我不是说= =
在 java 的对象标识运算符的意义上)示例
将产生小于 0 的值,因为
abc
按字母顺序排列在def
之前。将产生大于 0 的值,因为 2 大于 1。
一些附加点
注意:对于实现 Comparable 的类来说,在 javadoc 中声明其compareTo() 方法的语义是一种很好的做法。
注意:您应该至少阅读以下内容之一:
Sun Java 中的收藏踪迹
教程
Joshua Bloch,特别是第 12 条:
考虑实现 Comparable
Maurice Naftalin,Philip Wadler,第 3.1 章:可比较
警告:您永远不应该依赖compareTo 的返回值为
-1
、0
和1
。您应该始终测试x < 0
、x == 0
、x > 0 分别。
Official Definition
From the reference docs of Comparable.compareTo(T):
My Version
In short:
returns
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)Examples
will yield something smaller than 0 as
abc
is alphabetically beforedef
.will yield something larger than 0 because 2 is larger than 1.
Some additional points
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 Collection Trail in the Sun Java
Tutorial
Joshua Bloch, especially item 12:
Consider implementing Comparable
Maurice Naftalin, Philip Wadler, chapter 3.1: Comparable
Warning: you should never rely on the return values of compareTo being
-1
,0
and1
. You should always test forx < 0
,x == 0
,x > 0
, respectively.我使用这个助记符:
保留符号并始终将
compareTo()
的结果与 0 进行比较I use this mnemonic :
You keep the signs and always compare the result of
compareTo()
to 0简短回答:(搜索您的情况)
Answer in short: (search your situation)
举个例子,如果我们想比较“a”和“b”,即(“a”== this)
take example if we want to compare "a" and "b", i.e ("a" == this)
可用于排序,0表示“等于”,-1表示“更少”和“更多(更大)”。
任何小于 0 的返回值都意味着左操作数较小,如果值大于 0,则左操作数较大。
It can be used for sorting, and 0 means "equal" while -1, and 1 means "less" and "more (greater)".
Any return value that is less than 0 means that left operand is lesser, and if value is bigger than 0 then left operand is bigger.
compareTo()
方法返回一个具有以下特征的 int:如果 thisObject
anotherObject
如果 thisObject == anotherObject
如果 thisObject >另一个对象
The
compareTo()
method returns an int with the following characteristics:If thisObject < anotherObject
If thisObject == anotherObject
If thisObject > anotherObject
System.out.println(A.compareTo(B)>0?"Yes":"No")
如果 A>B 的值将返回“Yes”或“No”。
System.out.println(A.compareTo(B)>0?"Yes":"No")
if the value of A>B it will return "Yes" or "No".