How do you go about comparing two generic classes?
class Entry<K,V>
{
protected K key;
protected V value;
public K getKey() { return key; }
public V getValue() { return value; }
public static Comparator KeyComparator = new Comparator()
{
public int compare(Object o1, Object o2)
{
int key1 = ( (Entry) o1 ).getKey();
int key2 = ( (Entry) o2 ).getKey();
if (key1 > key2)
{
return 1;
}
else if (key1 < key2)
{
return -1;
}
else
{
return 0;
}
}
};
}
I get the following compile error:
int key1 = ( (Entry) o1 ).getKey();
^
int key2 = ( (Entry) o2 ).getKey();
^
incompatible types
found : java.lang.Object
required: int
K
and V
will be Integer
s used within a circular arraylist implementation. Is there a simpler way (or at least one that works) to compare like I normally would with int
?