Re: Interfaces Question - I am missing something
I understand that compareTo implements Comparable and therefore any
You're confusing your terminology a bit here. compareTo does not
implement Comparable, rather any object that implements the Comparable
interface must has a method called compareTo(Object other).
item that is a Comparable type can be passed to it, but how do I know
what compareTo really does? Beyond the signature in the Interface.
You don't know what it really does and that is the whole point of
using an interface! As long as the class that implements the
compareTo() method adheres to the documented behavior, then any method
that takes a Comparable object will work with an instance of any class
that implements Comparable.
The docs I read said it took a String str, but what if I had numbers?
I'm probably straying from the original point, but the "real"
compareTo method lives somewhere and has a whole lot of code in it,
right?
No. The "real" code is usually very short and exists as a specialized
routine for each class. Let me give you a concrete example with a
String-like class, an Integer-like class and a couple custom classes.
I am using the Generics parameterization, but don't let that throw
you.
I hope you can see that it is much easier to develop a compareTo
function for an object *within that class* rather than trying to write
some uber-compareTo() that knows about every object ever written.
Also, notice that you often want to impose different orderings on the
same underlying class. That is why many of the Collections classes
(like TreeSet) have a constructor that takes a Comparator object, that
way you can use a different compareTo method without having to
override anything.
/**
* Example 1
*/
public class MyString implements Comparable<MyString>
{
private char[] chars;
...
public int compareTo( MyString other )
{
// This compareTo function imposes an order on strings up to
the
// length of the shorter string
for ( int i = 0; i < Math.min( chars.length,
other.chars.length ); i++ ) {
if ( chars[i] < other.chars[i] )
return -1;
if ( chars[i] > other.chars[i] )
return 1;
}
// strings are equal
return 0;
}
}
/**
* Example 2
*/
public class MyInteger implements Comparable<MyInteger>
{
private int value;
...
public int compareTo( MyInteger other )
{
// This orders the integers from low to high
return value - other.value;
}
}
/**
* Example 3 : This is a Set-like class and sets are ordered by size
*/
public class MySet implements Comparable<MySet>
{
private Set theSet;
...
public int compareTo( MySet other )
{
// This orders the sets from smallest to largest
return theSet.size() - other.theSet.size();
}
}
/**
* Example 4 : Another Integer class, but now we sort by the number of
ones in the
* binary representation of the integer
*/
public class MyInteger2 implements Comparable<MyInteger2>
{
private int value;
...
public int compareTo( MyInteger2 other )
{
// Think about this one....it's good for you.
return Intger.bitCount( value ) -
Integer.bitCount( other.value );
}
}