Iterate through a HashMap
1954
04-May-2015
What's the best way to iterate over the items in a HashMap?
Anonymous User
04-May-2015public static void printMap(Map mp) {Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
for (Object value : map.values()) {// ...
}