I tried to iterate over hashmap in Java, which should be a fairly easy thing to do. However, the following code gives me some problems:
HashMap hm = new HashMap(
Using EntrySet() and for each loop
for(Map.Entry entry: hashMap.entrySet()) {
System.out.println("Key Of map = "+ entry.getKey() +
" , value of map = " + entry.getValue() );
}
Using keyset() and for each loop
for(String key : hashMap.keySet()) {
System.out.println("Key Of map = "+ key + " ,
value of map = " + hashMap.get(key) );
}
Using EntrySet() and java Iterator
for(String key : hashMap.keySet()) {
System.out.println("Key Of map = "+ key + " ,
value of map = " + hashMap.get(key) );
}
Using keyset() and java Iterator
Iterator keysIterator = keySet.iterator();
while (keysIterator.hasNext()) {
String key = keysIterator.next();
System.out.println("Key Of map = "+ key + " , value of map = " + hashMap.get(key) );
}
Reference : How to iterate over Map or HashMap in java