I want to write a generic Pair class, which has two members: key and value. The only requirement to this class is that both key and value should implements the Comparable interface, otherwise Pair class will not accept them as type parameter.
First I code it like this:
public class Pair<T1 extends Comparable, T2 extends Comparable>
But the JDK 1.6 compiler will generate warning about this:
Comparable is a raw type. References to generic type Comparable<T> should be parameterized
Then I tried to add type parameters and the code now looks like this:
public class Pair<T1 extends Comparable<? extends Object>,
T2 extends Comparable<? extends Object>>
Now everything go well until I tried to generate an Comparator for Pair.(The following code is in Pair class)
public final Comparator<Pair<T1, T2>> KEY_COMPARATOR = new Comparator<Pair<T1, T2>>() {
public int compare(Pair<T1, T2> first, Pair<T1, T2> second) {
*first.getKey().compareTo(second.getKey());*
return 0;
}
};
The code first.getKey().compareTo(second.getKey());
will generate an error saying:
The method compareTo(capture#1-of ? extends Object) in the type Comparable<capture#1-of ? extends Object> is not applicable for the arguments (T1)
Anyone knows what does this error message mean?
Any hints on this topic are welcome.
UPDATE:
Here is the complete code:
public class Pair<T1 extends Comparable<? extends Object>, T2 extends Comparable<? extends Object>> {
private T1 key;
private T2 value;
public static int ascending = 1;
public final Comparator<Pair<T1, T2>> KEY_COMPARATOR = new Comparator<Pair<T1, T2>>() {
public int compare(Pair<T1, T2> first, Pair<T1, T2> second) {
int cmp = first.getKey().compareTo((T1)(second.getKey()));
if (cmp > 0) return ascending;
return -ascending;
}
};
}
@MarvinLabs Can you explain a bit more why the compiler cannot make sure objects are compared to other objects of the same type. In the above code, second.getKey()
returns T1 type, which is of the same type as first.getKey()
See Question&Answers more detail:
os