On Java Development

All things related to Java development, from the perspective of a caveman.

Iterating through a Hashmap

without comments


Unlike Lists, and ArrayLists should come to mind, Maps don’t have an iterator() method like that provided by the Set or List classes. However, you can iterate through the keys or the key-value elements.

There are a few different types of maps such as HashMap, TreeMap and LinkedHashMap. The ordering of the elements in each differ a bit depending on the type of map.

The list below describe the main variables used in the method that follows.

The Java code below creates the HashMap, adds a key/value pair (or pairs) and then uses an iterator to loop through the map to extract the name/value pairs that are sent to the console.

This would print:
The key value within yourobj is NameAddr01
The value of the property within the object is Imgona Choitoya

This is the NameAndAddr class.


This is the entire test class containing the method used to iterate through the map.

Written by admin

March 28th, 2014 at 6:24 am

Leave a Reply

You must be logged in to post a comment.