java Map.Entry<K,V> 的比较器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17211291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:19:39  来源:igfitidea点击:

comparator for Map.Entry<K,V>

javamapcomparator

提问by Raghava

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

我有一个以枚举类型为键、以 Double 为值的 Map。我想根据 Double 值对其进行排序。所以我得到了条目集并想Collections.sort()与比较器一起使用。我有以下比较器代码

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).
  1. Syntax error on token ".", extends expected (line 1).
  2. The type parameter Map is hiding the type Map<K,V> (line 1).
  3. 由于上述两个错误,K 和 V 无法解析为类型(第 3,4 行)。

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

我无法解决这个问题。任何帮助都受到高度赞赏。提前致谢。

回答by Lukas Eder

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

ScoreComparatortakes two generic type arguments Kand V. Map.Entry<K, V>is not a valid generic type definition, but you may well use it to bind to Comparator<T>'s Ttype.

ScoreComparator接受两个泛型类型参数KV. Map.Entry<K, V>不是有效的泛型类型定义,但您可以很好地使用它来绑定到Comparator<T>T类型。

Note that Vmust extend Comparable<V>, in order to be able to call compareTo()on o1.getValue().

需要注意的是V必须扩展Comparable<V>,为了能够调用compareTo()o1.getValue()

You can now use the above ScoreComparatoras such:

您现在可以使用上述内容ScoreComparator

new ScoreComparator<String, String>();
new ScoreComparator<Long, Integer>();
// etc...

Note, from your current implementation, you probably don't even need the Kparameter. An alternative:

请注意,从您当前的实现来看,您可能甚至不需要该K参数。替代:

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