Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparator for Map.Entry<K,V>

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

  1. Syntax error on token ".", extends expected (line 1).
  2. The type parameter Map is hiding the type Map<K,V> (line 1).
  3. Because of the above two errors, K and V cannot be resolved to a type (lines 3,4).

I am unable to resolve this. Any help is highly appreciated. Thanks in advance.

like image 425
Raghava Avatar asked Jun 20 '13 10:06

Raghava


1 Answers

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());
    }   
}
like image 137
Lukas Eder Avatar answered Oct 06 '22 21:10

Lukas Eder