1

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?

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

                }
The Time
  • 697
  • 5
  • 12
  • 26

1 Answers1

12

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:

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:

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?

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255