Does the specific signed integer matter when implementing compareTo in a Comparable <Type> class?
Posted
by javanix
on Stack Overflow
See other posts from Stack Overflow
or by javanix
Published on 2010-04-13T23:05:08Z
Indexed on
2010/04/13
23:13 UTC
Read the original article
Hit count: 409
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)
© Stack Overflow or respective owner