I have a Map with an enumeration type as the key and Double as the value. I want to sort this based on the Double values. So I got the entry set and want to use Collections.sort() with a comparator. I have the following code for the comparator
class ScoreComparator<Map.Entry<K, V>> implements Comparator<Map.Entry<K, V>> {
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
return o1.getValue().compareTo(o2.getValue());
}
}
I am getting the following error messages
Syntax error on token ".", extends expected (line 1).The type parameter Map is hiding the type Map<K,V> (line 1).I am unable to resolve this. Any help is highly appreciated. Thanks in advance.
You probably want this:
// Declare K and V as generic type parameters to ScoreComparator
class ScoreComparator<K, V extends Comparable<V>>
// Let your class implement Comparator<T>, binding Map.Entry<K, V> to T
implements Comparator<Map.Entry<K, V>> {
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
// Call compareTo() on V, which is known to be a Comparable<V>
return o1.getValue().compareTo(o2.getValue());
}
}
ScoreComparator takes two generic type arguments K and V. Map.Entry<K, V> is not a valid generic type definition, but you may well use it to bind to Comparator<T>'s T type.
Note that V must extend Comparable<V>, in order to be able to call compareTo() on o1.getValue().
You can now use the above ScoreComparator as such:
new ScoreComparator<String, String>();
new ScoreComparator<Long, Integer>();
// etc...
Note, from your current implementation, you probably don't even need the K parameter. An alternative:
class ScoreComparator<V extends Comparable<V>>
implements Comparator<Map.Entry<?, V>> {
public int compare(Map.Entry<?, V> o1, Map.Entry<?, V> o2) {
// Call compareTo() on V, which is known to be a Comparable<V>
return o1.getValue().compareTo(o2.getValue());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With