How do I look through each element of a HashMap?
Created May 4, 2012
John Zukowski To go through all the elements of a HashMap, or any class that implements the Map interface, call the entrySet() or keySet() methods than loop through what is returned. The entrySet() returns a group of Map.Entry elements, whereas the keySet() method just returns a Set of key elements. If you then want what the key refers to, you'd have to look them up.
Once you have a Set to work with, you would then use an Iterator to go through all its elements. The following demonstrates:
Map map = some hash map Set set = map.keySet(); Iterator it = set.iterator(); while (it.hasNext()) { System.out.println(it.next()); }