java Map.Entry 是原始类型。对泛型类型 Map<K,V>.Entry<K,V> 的引用应该是

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30421070/
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-02 17:05:45  来源:igfitidea点击:

Map.Entry is a raw type. References to generic type Map<K,V>.Entry<K,V> should be

java

提问by The Time

I am assigning double key with an arrayList in a map and trying to retrieve the data from the map but I am getting this error below. How can I get it to work?

我在地图中使用 arrayList 分配双键并尝试从地图中检索数据,但我在下面收到此错误。我怎样才能让它工作?

Multiple markers at this line - Map.Entry is a raw type. References to generic type Map.Entry should be parameterized - Type mismatch: cannot convert from Object to Map.Entry

此行有多个标记 - Map.Entry 是原始类型。对泛型类型 Map.Entry 的引用应该被参数化 - 类型不匹配:无法从 Object 转换为 Map.Entry

Map<Double, ArrayList<Integer>> map = new HashMap<Double, ArrayList<Integer>>();
    else {

                Map<Double, ArrayList<Integer>> mapResult = db.detectRoute(latD, longD);
                Iterator it = mapResult.entrySet().iterator();
                while(it.hasNext()){
                    //The error starts here.
                    Entry e =  it.next();
                    double distance = entry.getkey();
                    ArrayList<Integer> value = entry.getValue();

                }

回答by JB Nizet

Well, stop using raw types:

好吧,停止使用原始类型:

Map<Double, ArrayList<Integer>> mapResult = db.detectRoute(latD, longD);
Iterator<Map.Entry<Double, ArrayList<Integer>>> it = mapResult.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry<Double, ArrayList<Integer>>> e =  it.next();
    Double distance = entry.getKey();
    ArrayList<Integer> value = entry.getValue();
}

Or simpler: use a foreach loop:

或者更简单:使用 foreach 循环:

Map<Double, ArrayList<Integer>> mapResult = db.detectRoute(latD, longD);
for (Map.Entry<Double, ArrayList<Integer>>> entry : mapResult.entrySet()) {
    Double distance = entry.getKey();
    ArrayList<Integer> value = entry.getValue();
}

Or even simpler, with Java 8:

或者更简单,使用 Java 8:

Map<Double, ArrayList<Integer>> mapResult = db.detectRoute(latD, longD);
mapResult.forEach((distance, value) -> {
    // ...
});

Also read What is a raw type and why shouldn't we use it?

另请阅读什么是原始类型,为什么我们不应该使用它?