How To Print HashMap In Java [Keys And Values]

Do you want to know how to print HashMap values and keys in JAVA? In this post, I’ll show you how to print the hash map’s values and keys in JAVA.

HashMap is an implementation of the Map interface that is used to group elements into key-value pairs. We can use a variety of methods to print its elements. Examples include keySet(), values(), entrySet(), and asList(). Let us look at some examples.

Ways to Print Values and Keys in JAVA for HashMap

As you may be aware, the keySet() method returns a set view of the map’s keys, while the values() method returns a set view of the map’s values.

As a result, we can use keySet() to print all keys in the map and values() to print all values. There are numerous ways to accomplish this.

To get a set of keys, we can use the keySet() function, and then in the for loop, we can use the get() method to get the value. The get() function returns the value associated with the provided key. Examine the example below.

Using Println function to Print HashMap in JAVA

Using this method is the simplest way to print HashMap in Java. Simply pass the HashMap reference to the println() method, and the key-value pairs will be printed inside the curly braces. Take a look at the example below.

import java.util.HashMap;
import java.util.Map;
 
class Main
{
    // Program to print all keys present in the map using `keySet()` in Java
    public static void main(String[] args)
    {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "First Element");
        hashMap.put(2, "Second Element");
      
        System.out.println(hashMap);
    }
}

Output:

{1=First Element, 2=Second Element}
Printing HashMap Values and Keys in JAVA

Using for-each loop To Print HashMap In Java

The for-each loop can be used by any object that implements the Iterable interface. Because Set extends Iterable, we can use a for-each loop to loop through the keySet and values.

import java.util.HashMap;
import java.util.Map;
 
class Solution
{
    // Program to print all keys present in the map using `keySet()` in Java
    public static void main(String[] args)
    {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "First Element");
        hashMap.put(2, "Second Element");

      
        // Using For Each loop to print HashMap
        for (Integer key: hashMap.keySet()) {
            System.out.println(key);
        }
    }
}

Output:

1
2

Using Iterator in Java 8 forEachRemaining()

The Iterator interface’s forEachRemaining() method can print each element until all of them have been processed.

import java.util.HashMap;
import java.util.Map;
 
class Solution
{
    // Program to print all keys present in the map using `keySet()` in Java
    public static void main(String[] args)
    {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "First Element");
        hashMap.put(2, "Second Element");
 
       // Using ForEachRemaining for printing HashMap
       hashMap.keySet().iterator()
                .forEachRemaining(System.out::println);
    }
}

Output:

1
2

Using Stream.forEach in Java 8

To print each stream element, we can use the Stream.forEach() method to loop through the keySet and values.

import java.util.HashMap;
import java.util.Map;
 
class Solution
{
    // Program to print all keys present in the map using `keySet()` in Java
    public static void main(String[] args)
    {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "First Element");
        hashMap.put(2, "Second Element");
 
        // Using Java 8 – Collection.stream() + Stream.forEach() to print the HashMap
        hashMap.keySet().stream()
                .forEach(System.out::println);
    }
}

Output:

1
2

Using toString method ()

To display all keys and values on the map, we can simply print the string representations of keySet() and values().

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
 
class Solution
{
    // Program to print all keys present in the map using `keySet()` in Java
    public static void main(String[] args)
    {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "First Element");
        hashMap.put(2, "Second Element");
 
        // Using Convert to a string method to print HashMap
        System.out.println(hashMap.keySet().toString());
 
        // Java 8
        Stream.of(hashMap.keySet().toString())
                .forEach(System.out::println);
    }
}

Output:

[1, 2]
[1, 2]

Using Simple Iterator

Map does not have its own iterator because it does not extend the Collection interface. Because the set extends the Collection interface, we can get an iterator from both keySet() and values().

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
class Main
{
    // Program to print all keys present in the map using `keySet()` in Java
    public static void main(String[] args)
    {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "First Element");
        hashMap.put(2, "Second Element");
 
        // Using an iterator for Printing the HashMap
        Iterator<Integer> mapIterator = hashMap.keySet().iterator();
        while (mapIterator.hasNext()) {
            System.out.println(mapIterator.next());
        }
     
    }
}

Output:

1
2

Wrap Up

I hope you now have a better understanding of how to print a hashmap in Java. I’ve provided more than five methods that you can use to print the keys and values of a HashMap in Java, which you can find in the code below.

Please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog. Wanna read more interview-related questions? Check Other Post Related to Data Structures.

Leave a Comment