How to Iterate through Hashmap

In this article we will focus on how to iterate through hashmap. There are many ways to iterate through HashMap’s in java. We will try some of them.

Methods to iterate through hashmap

Using for-each loop we can loop through the keySet or the values or the entrySet by the following methods

Iterators are also used to loop through the hashmaps.

For example we will take a Hashmap and add data to it

Display keys

Display values

Display Keys and values using keySetand getmethod of hashMap

Display key and values using entrySet() method

Display key and values using Iterator

Below is the complete java program that has all the methods to loop through a Hashmap and display keys/values or a key value pair.

Output of the Code is

Iterate through Hashmap

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn