How do I look through each element of a HashMap?

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()) {
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact