comparator for Map.Entry<K,V>

爱⌒轻易说出口 提交于 2019-12-12 07:39:51

问题


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.


回答1:


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());
    }   
}


来源:https://stackoverflow.com/questions/17211291/comparator-for-map-entryk-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!