Map.Entry是原始类型。对泛型类型Map <k,v> .Entry <k,v>的引用应该是

时间:2015-05-24 07:25:19

标签: java

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

  

此行有多个标记        - 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();

                }

1 个答案:

答案 0 :(得分:9)

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

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

或者更简单:使用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();
}

甚至更简单,使用Java 8:

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

另请阅读What is a raw type and why shouldn't we use it?