Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the specific signed integer matter when implementing compareTo in a Comparable <Type> class?

When implementing compareTo(), does the degree of "difference" need to be taken into account?

For instance, if I have 3 objects, C1, C2, and C3, such that C1 < C2 < C3.

Should C1.compareTo(C2) return an integer that is less than C2.compareTo(C3)?

The documentation for the Comparable interface doesn't seem to specify one way or another, so I'm guessing the degree doesn't matter, but it would be nice to know if there is some advantage returning a specific number (for example, improving TreeSet sort speed or something).

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html#compareTo(T)

like image 215
javanix Avatar asked Apr 13 '10 23:04

javanix


People also ask

Does integer implement comparable?

All the common numbers (Integer, Long, Double, etc) do implement Comparable. You can still call Collections. sort as long as the elements themselves implement Comparable.

When a class implements to comparable interface what method must also be implemented?

For any class to support sorting, it should implement the Comparable interface and override it's compareTo() method. It returns a negative integer if an object is less than the specified object, returns zero if an object is equal, and returns a positive integer if an object is greater than the specified object.

How does compareTo in comparable work in Java?

The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The method returns 0 if the string is equal to the other string.

What is difference between compareTo and comparable in Java?

Comparable in Java is an object to compare itself with another object, whereas Comparator is an object for comparing different objects of different classes. Comparable provides the compareTo() method to sort elements in Java, whereas Comparator provides compare() method to sort elements in Java.


2 Answers

Interesting question, but nonetheless no, the magnitude of the int has no significance as per Comparable<T> and Comparator<T> specifications, only the sign. Conceivably some sorting algorithm can additionally specify that they can take "hints" from the magnitude, but I'm not sure how practical that would be for comparison-based sorting, since we really need only to know if a < b, a == b, or a > b (which is really what Comparable and Comparator are OOP abstractions of).


Now it needs to be said that there may be a hidden intention here of using the subtraction idiom for comparing numeric values, i.e. something like this:

public int compare(T t1, T t2) {
    return t1.intField - t2.intField;
}

Do note that this comparison method is potentially broken, due to possible overflow when the difference between the two numbers is greater than Integer.MAX_VALUE. In fact, this is one of the puzzles covered in Java Puzzlers.

To demonstrate, consider the following snippet (taken from the book):

int x = -2000000000;
int z = 2000000000;
System.out.println(x - z); // prints a positive number due to overflow

Clearly x < z, and yet x - z is a positive number. Beware of using this subtraction idiom: it's always much safer to do an explicit comparison and return -1, 0, or 1 instead.

like image 127
polygenelubricants Avatar answered Nov 13 '22 22:11

polygenelubricants


no, the only difference is between negative numbers, 0, and positive numbers. the degree is irrelevant.

like image 25
chris Avatar answered Nov 13 '22 21:11

chris