Printing HashMap Values and Keys in JAVA

Are you looking for ways on how to Print HashMap values and keys in JAVA? In this post, I will show you how you can print all the values and keys of the hash map in JAVA.

HashMap is a Map interface implementation class that is used to group elements into key-value pairs. To print its elements, we can utilize a variety of approaches. Methods such as keySet(), values(), entrySet(), and asList() are examples. Let’s have a look at some examples.

Ways to Print Values and Keys in JAVA for HashMap

As you already know that the keySet() method returns a set view of the map’s keys, and the values() method returns a set view of the map’s values.

As a result, we can use keySet() to print all keys and values() to print all values in the map. There are numerous options for doing so.

To get a set of keys, we can use the keySet() function, and then use the get() method in the for loop to acquire the value. The value associated with the provided key is returned by the get() function. Take a look at the sample below.

Using Println function to Print HashMap in JAVA

The simplest approach to print HashMap in Java is to use this method. Simply supply the HashMap reference to the println() method, and the key-value pairs will be printed inside the curly braces. Take a look at the sample below.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
 
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);
    }
}

Use a for-each loop.

Any object that implements the Iterable interface can use the for-each loop. We can use a for-each loop to loop through the keySet and values because Set extends Iterable.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
 
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 For-each loop to print HashMap
        for (Integer key: map.keySet()) {
            System.out.println(key);
        }
    }
}

Iterator in Java 8.forEachRemaining()

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

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
 
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 ForEachRemaining for printing HashMap
        map.keySet().iterator()
                .forEachRemaining(System.out::println);
    }
}

Stream.forEach in Java 8 ()

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

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
 
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 Java 8 – Collection.stream() + Stream.forEach() to print the HashMap
        map.keySet().stream()
                .forEach(System.out::println);
    }
}

Using the toString method ()

We can just print the string representations of keySet() and values() to display all keys and values existing on the map.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
 
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 Convert to a string method to print HashMap
        System.out.println(map.keySet().toString());
 
        // Java 8
        Stream.of(map.keySet().toString())
                .forEach(System.out::println);
    }
}

Using Iterator

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

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
 
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 = map.keySet().iterator();
        while (mapIterator.hasNext()) {
            System.out.println(mapIterator.next());
        }
     
    }
}

If you liked our post related to the hash map implementation in JAVA then 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.

Share your love

One comment

Leave a Reply