Print Elements of a HashMap in Java



In Java, a HashMap is a subclass of the AbstractMap class and is used to store key-value pairs. Each key in the map is mapped to a single value in the map, and the keys are unique.

Printing Java HashMap Elements 

We can insert a key only once in a map, and duplicate keys are not allowed, but the value can be mapped to multiple keys. We can add the elements using the put() method of the HashMap class and iterate over the elements using the Iterator interface.

Java HashMap provides various ways to print its elements as follows:

Using for-each loop with keySet()

The for-each loop iterates over an array, list, or collection, and the keySet() method returns a Set view of the keys contained in this map. By using this method within the for-each loop, we can access all keys of the HashMap element.

If you pass the key to the get() method, it will return the HashMap element.

Example

The following example uses the for-each loop and keySet() method to print all the elements of the HashMap {"1": "10", "2": "20", "3": "30", "4": "40", "5": "50"}:

import java.util.HashMap;
public class printHashMap {
   public static void main(String[] args) {
      //create HashMap
      HashMap<Integer, Integer> my_map = new HashMap<>();
      
      //adding element to it
      my_map.put(1, 10);
      my_map.put(2, 20);
      my_map.put(3, 30);
      my_map.put(4, 40);
      my_map.put(5, 50);
      
      //print HashMap elements
	  System.out.println("HashMap elements are: ");
      for(Integer m : my_map.keySet()){
         System.out.println("key = " + m + ", Value = " + my_map.get(m));
      }
   }
}

The above program displays all the elements of HashMap:

HashMap elements are: 
key = 1, Value = 10
key = 2, Value = 20
key = 3, Value = 30
key = 4, Value = 40
key = 5, Value = 50

Using Iterator Object

In Java, an Iterator is an object that is used to loop through collection objects, such as HashSet, ArrayList, etc. It provides a method named iterator(), which retrieves an iterator.

Example

In this example, we use an Iterator object to iterate over a HashMap and use the next() and getValue() methods inside a while loop to print each element of the HashMap one by one:

import java.util.*;
import java.util.Map.Entry;

public class HashMapTest {
   public static void main(String[] args) {
      // Creating a HashMap
      HashMap<Integer, String> map = new HashMap<>();

      // Adding elements to it
      map.put(1, "Adithya");
      map.put(2, "Jai");
      map.put(3, "Chaitanya");
      map.put(4, "Krishna");
      map.put(5, "Ramesh");

      if (!map.isEmpty()) {
         Iterator<Entry<Integer, String>> it = map.entrySet().iterator();

         System.out.println("HashMap elements are: ");
         while (it.hasNext()) {
            Map.Entry<Integer, String> obj = it.next();
            System.out.println("Key = " + obj.getKey() + ", Value = " + obj.getValue());
         }
      }
   }
}

Following is the output of the above program:

HashMap elements are: 
Key = 1, Value = Adithya
Key = 2, Value = Jai
Key = 3, Value = Chaitanya
Key = 4, Value = Krishna
Key = 5, Value = Ramesh
Updated on: 2025-06-18T18:53:34+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements