-1

I'm trying to loop through a HashMap with a String as key and an Object of my Class as value. I want rek1 to have key "A" and rek2 key "B".

Here's my code:

private Map<String, List<X2Y2>> areaMap = new HashMap<String, List<X2Y2>>();
ArrayList<X2Y2> arrList = new ArrayList<X2Y2>();
X2Y2 rek1 = new X2Y2(1.0,1.0, 2.0, 2.0);
X2Y2 rek2 = new X2Y2(3.0,3.0, 4.0, 4.0);
arrList.add(rek1);
arrList.add(rek2);
areaMap.put("A", arrList);

for (Object key : areaMap.keySet()) {
   String lKey = (String) key;
   List<X2Y2> list = areaMap.get(key);
   Log.i("Worku?", list.toString());
}

class X2Y2(var x1: Double, var y1: Double, var x2: Double, var y2: Double) {}

The thing is that I can only get the key value, not the list with my X2Y2. Anyone got any tip that can help me get my X2Y2 values ?

GBlodgett
  • 12,704
  • 4
  • 31
  • 45

1 Answers1

3

change areaMap.get(key); to areaMap.get(lKey);

I do think you should iterate using a entry set so that you could get the value easier though

for (Map.Entry<String, List<X2Y2>> kv : areaMap.entrySet()) {
    System.out.println(kv.getValue());
}
scigs
  • 529
  • 1
  • 5
  • 12
  • System.out: [io.agilevision.proximity.indoor.X2Y2@802c399, io.agilevision.proximity.indoor.X2Y2@21c165e] is the output i get.. Do i just need to loop through the list after that or what? – Dennis Lipponen May 04 '18 at 14:10
  • yeah, you could do a `kv.getValue().forEach(System.out::println);` if you want to print out the values or do whatever with them – scigs May 04 '18 at 14:13