Last Updated : 10 Apr, 2024
HashMap<String, Integer> hm = new HashMap<String, Integer>();
hm.put(\"First\", 1);
hm.put(\"Second\", 2);
hm.put(\"Third\", 3);
for(Map.Entry itr: hm.entrySet())
System.out.println(itr.getKey() + \" \" + itr.getValue()); 

Possible outcome for the above code snippet
(A) First 1
Second 2
Third 3
(B) Third 3
Second 2
First 1
(C) Second 2
Third 3
First 1
(D) None of these


Answer: (C)

Explanation:
This class makes no guarantees as to the order of the map;
In particular, it does not guarantee that the order will remain constant over time.
Map.Entry iterator_name: Map_name.entrySet():- iterates the map.
getKey(): returns the key while traversing
getValue(): returns the value associated with the corresponding key.



Share your thoughts in the comments