Java

How to Print HashMap in Java

HashMap is a Map implementation that uses hash tables. It is an unsorted, unordered collection of key-value pairs. Like Map, HashMap also stores data in the form of key-value pairs. Every key-value pair is contained in curly brackets { } and are separated by commas “,”. The keys are unique, and each key is mapped to a single value. This means a key can only be inserted into a map at once, and duplicate keys are unacceptable.

This tutorial will help you to print HashMap in Java.

How to Print HashMap in Java?

You can print HashMap in Java using:

Let’s understate the working of each method one by one!

Method 1: Print HashMap in Java Using print Statement

For printing HashMap in Java, the simplest and most basic method is using print statements. If you pass the HashMap reference to the “System.out.println()” method, the HashMap will display the key-value pairs for the elements wrapped in curly brackets.

Syntax
Follow the given syntax for printing HashMap with the help of the System.out.println() method:

System.out.println(MapVariable);

Example
In this example, first, we will create a HashMap named “vehicles”:

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

Then, we will add key-value pairs in HashMap using “put()” method:

vehicles.put(1, "Cycle");
vehicles.put(10, "Motor Bike");
vehicles.put(11, "Car");
vehicles.put(15, "Bus");
vehicles.put(18, "Truck");

Print the HashMap by passing the created HashMap in the System.out.println() method:

System.out.println(vehicles);

The output print out the key-value pairs of HashMap:

Let’s try to print values of HashMap in vertical format.

Method 2: How to Print HashMap in Java Using entrySet() Method

Another method that can be utilized to print the HashMap is the “entrySet()” method. This method returns a set containing the map’s entries, where the set entries are the objects of the Map.Entry type.

Syntax
Use the following syntax to print a HashMap by utilizing the entrySet() method:

for( Map.Entry<Integer, String> entry : MapVariable.entrySet() ){
    System.out.println( entry.getKey() + " = " + entry.getValue() );
}

Example
Here, we will print the same HashMap “vehicles” using the “entrySet()” method. This method will print the key-value pairs in vertical order:

for( Map.Entry<Integer, String> entry : vehicles.entrySet() ){
    System.out.println( entry.getKey() + " = " + entry.getValue() );
}

Output

If you want to only print the keys of the HashMap, then follow the below section.

Method 3: How to Print HashMap in Java Using keySet() Method

To print the HashMap keys, you can utilize the “keySet()” method of the HashMap class. This method returns the set of all HashMap keys.

Syntax
The following syntax is used for printing keys of the HashMap with the keySet() method:

for(Integer keys : MapVaribale.keySet()){
    System.out.println( keys );
}

Example
Here, we will print all the keys of the created HashMap by calling the “keySet()” method in the “for” loop. We will create an integer type variable named “keys” that store the resultant keys of HashMap. Then, pass the keys variable to the “System.out.println()” method for printing them on the console:

for(Integer keys : vehicles.keySet()){
    System.out.println( keys );
}

As a result, only keys of the created HashMap will be displayed on the console:

Only want to get the values of the HashMap? Follow the given section.

Method 4: How to Print HashMap in Java Using values() Method

You can use the “values()” and “keySet()” methods to print the values and keys of HashMap separately. The values() method returns the entire set of values, whereas the keySet() method returns the entire set of HashMap keys.

Syntax
Use the below-given syntax for printing the values of the HashMap using values() method:

for(String value : MapVariable. values()){
    System.out.println( value );
}

Example
By invoking the values() method in the “for” loop, we will print all the values from the already created HashMap. The resultant values of the HashMap from the “values()” method will be stored in a String type variable with the name “value“. Then, to print out all of the values from the HashMap, pass the value variable to the System.out.println() method:

for(String value : vehicles.values()){
     System.out.println( value );
}

The output shows all the values of the HashMap “vehicles”:

Now, let’s move to the last method for printing the HashMap that is mostly used to print both keys and values.

Method 5: How to Print HashMap in Java Using forEach Loop

forEach()” loop is also utilized to print HashMap elements using the getKey() and getValue() methods. The getKey() method returns an entrySet key, and the getValue() method outputs the value associated with the key.

Syntax
For printing HashMap using a forEach loop, follow the below-given syntax:

MapVariable.entrySet().forEach(entry->{
    System.out.println(entry.getKey() + " = " + entry.getValue());  
});

Example
In this example, we will print the previously created HashMap “vehicles” by utilizing the “forEach” loop with lambda expression. Here, we first call the entrySet() method that returns all the entries of the map and then use the forEach loop to print both keys and values of the map by passing getKey() and getValue() methods to get the keys and values set of the HashMap:

vehicles.entrySet().forEach(entry->{
    System.out.println(entry.getKey() + " = " + entry.getValue());  
});

The output displays the hashMap keys and values:

We have compiled all the methods to print the hashmap in Java.

Conclusion

For printing elements of Hashmap in Java, there are some methods that you can use: print statement, entrySet() method, keySet() method, values() method, and forEach loop. The HashMap also stores data in key-value pairs, where keys are stored in the HashMaps using a hashtable and bucketing logic, and have a respective value. This tutorial discussed the methods to print a hashmap in Java with detailed examples.

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.