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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3767090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:38:13  来源:igfitidea点击:

What do the return values of Comparable.compareTo mean in Java?

javacomparable

提问by Magggi

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

是什么返回的区别0,返回1和返回-1compareTo()Java中?

回答by Sean Patrick Floyd

Official Definition

官方定义

From the reference docs of Comparable.compareTo(T):

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

将此对象与指定的对象进行比较以进行排序。当此对象小于、等于或大于指定对象时,返回一个负整数、零或正整数。

实现者必须确保所有 x 和 y 的 sgn(x.compareTo(y)) == -sgn(y.compareTo(x))。(这意味着 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 接口并违反此条件的类都应该清楚地表明这一事实。推荐的语言是“注意:这个类有一个与 equals 不一致的自然顺序。”

在前面的描述中,符号sgn(expression)表示数学符号函数,其被定义为根据表达式的值是负、零还是正返回-1、0或1之一。

My Version

我的版本

In short:

简而言之:

this.compareTo(that)

returns

返回

  • a negative intif this < that
  • 0if this == that
  • a positive intif this > that
  • 如果 this < that,则为负整数
  • 0如果这个 == 那个
  • 如果 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)

这个方法的实现决定了<>and的实际语义==(我不是指==java 的对象标识运算符的意思)

Examples

例子

"abc".compareTo("def")

will yield something smaller than 0 as abcis alphabetically before def.

将按abc字母顺序产生小于 0 的值def

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

will yield something larger than 0 because 2 is larger than 1.

将产生大于 0 的值,因为 2 大于 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.

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

Note:you should read at least one of the following:

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

Warning:you should never rely on the return values of compareTo being -1, 0and 1. You should always test for x < 0, x == 0, x > 0, respectively.

警告:你永远不应该依赖 compareTo 的返回值是-1,01。你应该总是测试x < 0x == 0x > 0分别。

回答by Micha? Niklas

It can be used for sorting, and 0 means "equal" while -1, and 1 means "less" and "more (greater)".

可用于排序,0表示“相等”而-1,1表示“更少”和“更多(更大)”。

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.

任何小于 0 的返回值表示左操作数较小,如果值大于 0 则左操作数较大。

回答by Colin Hebert

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

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

回答by bhavesh

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

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

  1. negative int if a < b
  2. if a == b
  3. Positive int if a > b
  1. 如果 a < b 则为负整数
  2. 如果 a == b
  3. 如果 a > b,则为正整数

回答by Artur Yolchyan

int x = thisObject.compareTo(anotherObject);

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

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

  • negative If thisObject < anotherObject
  • zero If thisObject == anotherObject
  • positive If thisObject > anotherObject
  • 消极的 If thisObject < anotherObject
  • If thisObject == anotherObject
  • 积极的 If thisObject > anotherObject

回答by user1012506

Answer in short: (search your situation)

简而言之:(搜索您的情况)

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