Java

How to Print a Map in Java

A map holds values based on the key. So, it is also known as a key-value pair. The term “entry” refers to each key-value pair. The order of a map is determined by the implementation of LinkedHashMap, HashMap, and TreeMap. It also contains methods that can be utilized to perform different operations. The real-time examples of mapping are a map of managers and employees, a map of classes and students, and the map of the alphabet and the words in dictionaries.

This write-up will specifically demonstrate the method of printing a map in Java.

How to Print a Map in Java?

To print a map in Java, you can use:

  • for-each loop
  • Iterator

We will now check out each of the above-mentioned methods one by one.

Method 1: Print Map in Java Using for-each Loop

Within the for-each loop, you can use Map’s getValue() and getKey() methods to iterate over key-value pairs. This is the most systematic way to print a map and should be used if it is required to print both map keys and values.

To understand the concept, look at the example presented below.

Example
First, we will create a map object named “newMap” and specify “Integer” and “String” as the Key-value pair, which indicates the key is of Integer type and the value is of String type:

Map<Integer,String> newMap =new HashMap<Integer,String>();

Next, we will add the following three key-value pair in the Map object by using the “put()” method:

newMap.put(1,"Computer");  
newMap.put(2,"Laptop");  
newMap.put(3,"Mobile");

To print the created map object, we will utilize the “for-each” loop and invoke the “getKey()” and “getValue()” methods to retrieve the keys and their respective values:

for(Map.Entry mp: newMap.entrySet()){  
System.out.println(mp.getKey()+ " "+mp.getValue());  
}

Output

Let’s head towards the second method!

Method 2: Print Map in Java Using an Iterator

As map does not extend the Collection interface, that’s why it lacks its own iterator. However, “Map.entrySet()” returns a set of key-value pairs, and this method extends the Collection interface, which can be utilized for iteration.

Example
Here, we will use the same Map object created in the above example. Now, we will create the Iterator object named “itr”, which will contain the key-value pairs of the map object retrieved after the iteration. Lastly, we will utilize the “while” loop to print the Map object key-value pair on console:

Iterator<Entry> itr = newMap.entrySet().iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }

Output

We presented the basic information related for printing map in Java.

Conclusion

Java allows us to print a map using an iterator method or a for-each loop. The most commonly used method to print the Map in Java is the for-each loop as it iterates through key-value pairs by using the getKey() and getValue() methods of Map. The iterator method is almost the same for each loop but uses the iterator of the entrySet() method. This write-up showed you how to print a Map using Java.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.