How to Iterate a HashMap in Java?

Amansingh Javatpoint
1 min readJul 26, 2021

Hash-map is a class of Java collections framework which has the functionality to implement the Map interface of Java. It stores the data in key-value pairs and can be accessed by a different type of index. The keys are unique identifiers that are associated with the values of a map. Here, one object is key, also called an index, and the other object associated with the key is called value. When we try to add the duplicate key, it will automatically replace the corresponding key data.

In this tutorial, we will understand how to iterate over a HashMap using different ways.

To use the HashMap class and its methods, we have to import the java.util.HashMap package. The HashMap class implements the Map interface.

In order to iterate a HashMap in Java, we have to understand how to create it first.

The syntax to create a HashMap is as follows:

Map<K, V> hm_name = new HashMap<>();

--

--