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

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

+51
java comparable
Sep 22 '10 at 7:20
source share
6 answers

Official definition

From the reference documents Comparable.compareTo (T) :

Compares this object with the specified object for the order. Returns a negative integer, zero, or a positive integer because this object is less than, equal to, or greater than the specified object.

The developer must provide sgn (x.compareTo (y)) == -sgn (y.compareTo (x)) for all x and y. (This means that x.compareTo (y) should throw an exception; iff y.compareTo (x) throws an exception.)

The developer must also ensure that the relation is transitive: (x.compareTo (y)> 0 & y.compareTo (z)> 0) implies x.compareTo (z)> 0.

Finally, the developer must ensure that x.compareTo (y) == 0 means that sgn (x.compareTo (z)) == sgn (y.compareTo (z)), for all z.

It is highly 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. Recommended language: "Note: this class has a natural order that is incompatible with peers."

In the above description, the notation sgn (expression) denotes the mathematical function signum, which is defined to return one of -1, 0, or 1, depending on whether the value of the expression is negative, zero, or positive.

My version

In short:

 this.compareTo(that) 

returns

  • negative int if this <what
  • 0 if it == that
  • positive int if it> what

where the implementation of this method determines the actual semantics of < > and == (I do not mean == in the sense of the identification operator of a Java object)

Examples

 "abc".compareTo("def") 

will give something less than 0, since abc is in alphabetical order before def .

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

will give something more than 0 because 2 is greater than 1.

Some additional points

Note. . Good practice for a class implementing Comparable to declare semantics of the compareTo () method in javadocs.

Note: at least one of the following values ​​should be read:

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

+78
Sep 22 '10 at 7:22
source share

I use this mnemonic:

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

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

+39
Sep 22 2018-10-22
source share

Answer briefly: (find your situation)

  • 1 .compareTo ( 0 ) (return: 1 )
  • 1 .compareTo ( 1 ) (return: 0 )
  • 0 .comapreTo ( 1 ) (return: -1 )
+16
Sep 26 '16 at 5:52
source share

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

  • negative int if a <b
  • if a == b
  • Positive int if a> b
+5
Apr 08 '13 at 6:08 on
source share

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

Any return value less than 0 means that the left operand is less, and if the value is greater than 0, then the left operand is greater.

+3
Sep 22 '10 at 7:23
source share
 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
+1
Nov 17 '14 at 16:51
source share



All Articles