Java 中 Comparable.compareTo 的返回值是什么意思?

发布于 2024-09-24 14:01:24 字数 221 浏览 3 评论 0 原文

What is the difference between returning 0, returning 1 and returning -1 in compareTo() in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凉栀 2024-10-01 14:01:24

官方定义

来自 的参考文档Comparable.compareTo(T):

将此对象与
指定订购对象。返回一个
负整数、零或正数
整数,因为该对象小于,
等于或大于
指定对象。

实施者必须确保
sgn(x.compareTo(y)) ==
-sgn(y.compareTo(x)) 对于所有 x 和 y。 (这意味着 x.compareTo(y) 必须
当且仅当 y.compareTo(x) 时抛出异常
抛出异常。)

实施者还必须确保
该关系是传递的:
(x.compareTo(y)>0 && y.compareTo(z)>0)
意味着 x.compareTo(z)>0。

最后,实施者必须确保
x.compareTo(y)==0 意味着
sgn(x.compareTo(z)) ==
sgn(y.compareTo(z)),对于所有 z。

强烈推荐,但不推荐
严格要求
(x.compareTo(y)==0) == (x.equals(y))。
一般来说,任何一个类
实现Comparable接口
并违反此条件应
明确表明这一事实。这
推荐的语言是“注意:这
类有一个自然顺序,即
与等于不一致。”

在前面的描述中,
符号 sgn(表达式) 指定
数学符号函数,
它被定义为返回 -1 之一,
根据值是否为 0 或 1
表达式为负数、零或
积极。

我的版本

简而言之:

this.compareTo(that)

返回

  • 如果负整数,则 如果
  • 如果 this == 那 0
  • this > 一个正整数 该方法的实现决定

< >== 的实际语义(我不是说 = = 在 java 的对象标识运算符的意义上)

示例

"abc".compareTo("def")

将产生小于 0 的值,因为 abc 按字母顺序排列在 def 之前。

Integer.valueOf(2).compareTo(Integer.valueOf(1))

将产生大于 0 的值,因为 2 大于 1。

一些附加点

注意:对于实现 Comparable 的类来说,在 javadoc 中声明其compareTo() 方法的语义是一种很好的做法。

注意:您应该至少阅读以下内容之一:

警告:您永远不应该依赖compareTo 的返回值为 -1 01。您应该始终测试 x < 0x == 0x > 0 分别。

Official Definition

From the reference docs of 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.

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:

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.

鱼忆七猫命九 2024-10-01 14:01:24

我使用这个助记符:

a.compareTo(b) < 0 // a < b

a.compareTo(b) > 0 // a > b

a.compareTo(b) == 0 // a == b

保留符号并始终将 compareTo() 的结果与 0 进行比较

I use this mnemonic :

a.compareTo(b) < 0 // a < b

a.compareTo(b) > 0 // a > b

a.compareTo(b) == 0 // a == b

You keep the signs and always compare the result of compareTo() to 0

独行侠 2024-10-01 14:01:24

简短回答:(搜索您的情况)

  • 1.compareTo(0) (return: 1)
  • 1.compareTo(1) (返回: 0)
  • 0.comapreTo( >1)(返回-1

Answer in short: (search your situation)

  • 1.compareTo(0) (return: 1)
  • 1.compareTo(1) (return: 0)
  • 0.comapreTo(1) (return: -1)
会发光的星星闪亮亮i 2024-10-01 14:01:24

举个例子,如果我们想比较“a”和“b”,即(“a”== this)

  1. 负整数,如果a < b
  2. 如果 a == b
  3. 正 int 如果 a >乙

take example if we want to compare "a" and "b", i.e ("a" == this)

  1. negative int if a < b
  2. if a == b
  3. Positive int if a > b
冷情妓 2024-10-01 14:01:24

可用于排序,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.

她如夕阳 2024-10-01 14:01:24
int x = thisObject.compareTo(anotherObject);

compareTo() 方法返回一个具有以下特征的 int:

  • 如果 thisObject anotherObject
  • 如果 thisObject == anotherObject
  • 如果 thisObject >另一个对象
int x = thisObject.compareTo(anotherObject);

The compareTo() method returns an int with the following characteristics:

  • negative If thisObject < anotherObject
  • zero If thisObject == anotherObject
  • positive If thisObject > anotherObject
红尘作伴 2024-10-01 14:01:24

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文