Print HashMap in Java

Mohammad Irfan Jan 30, 2023 Sep 19, 2020
  1. Print HashMap Elements in Java
  2. Print HashMap Elements Using the keySet() Method in Java
  3. Print HashMap Elements Using forEach() Method in Java
  4. Print HashMap Elements Using Arrays.asList() in Java
  5. Print HashMap Elements Using Collections Class in Java
  6. Print HashMap Elements Using the entrySet() Method in Java
  7. Print HashMap Elements Using values() and keySet() Method in Java
  8. Print HashMap Elements Using Biconsumer in Java
Print HashMap in Java

This tutorial introduces how to print HashMap elements in Java.

HashMap is an implementation class of Map interface that is used to collect elements into key and value pairs. We can use various methods to print its elements. For example, keySet() method, values() method, entrySet() method, asList() method, etc. Let’s see some examples.

This is the simplest way to print HashMap in Java. Just pass the reference of HashMap into the println() method, and it will print key-value pairs into the curly braces. See the example below.

import java.util.HashMap;
import java.util.Map;

public class SimpleTesting{
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "Ten");
		map.put(100, "Hundred");
		map.put(1000, "Thousand");
		System.out.println(map);
	}
}

Output:

{100=Hundred, 1000=Thousand, 10=Ten}

We can use the keySet() method to get a set of keys and then get the value using the get() method in the for loop. The get() method returns the value associated with the passed key. See the example below.

import java.util.HashMap;
import java.util.Map;

public class SimpleTesting{
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "Ten");
		map.put(100, "Hundred");
		map.put(1000, "Thousand");
		for (Integer key: map.keySet()){
			System.out.println(key+ " = " + map.get(key));
		} 
	}
}

Output:

100 = Hundred
1000 = Thousand
10 = Ten

From Java 8, we can use forEach() method to print HashMap elements with the help of getKey() and getValue() method. The getKey() method returns a key from the entrySet and getValue() method returns value associated with the key. See the example below.

import java.util.HashMap;
import java.util.Map;

public class SimpleTesting{
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "Ten");
		map.put(100, "Hundred");
		map.put(1000, "Thousand");
		map.entrySet().forEach(entry->{
			System.out.println(entry.getKey() + " = " + entry.getValue());  
		}); 
	}
}

We can use Arrays.asList() method to print the HashMap elements. The asList() method returns a list representation of the HashMap. See the example below.

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class SimpleTesting{
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "Ten");
		map.put(100, "Hundred");
		map.put(1000, "Thousand");
		System.out.println(Arrays.asList(map));
	}
}

Output:

[{100=Hundred, 1000=Thousand, 10=Ten}]

We can use Collections.singletonList() static method to print the HashMap elements. The singletonList() method returns a list representation of the HashMap. See the example below.

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class SimpleTesting{
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "Ten");
		map.put(100, "Hundred");
		map.put(1000, "Thousand");
		System.out.println(Collections.singletonList(map));
	}
}

Output:

[{100=Hundred, 1000=Thousand, 10=Ten}]

The entrySet() method returns a set of entries that can be used in the for loop to print HashMap elements. See the example below.

import java.util.HashMap;
import java.util.Map;

public class SimpleTesting{
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "Ten");
		map.put(100, "Hundred");
		map.put(1000, "Thousand");
		for (Map.Entry<Integer, String> entry : map.entrySet()) {
		    System.out.println(entry.getKey()+" = "+entry.getValue());
		}
	}
}

Output:

100 = Hundred
1000 = Thousand
10 = Ten

If we want to print values and keys independently, we can use the values() and keySet() method. The values() method returns a list of all the values, whereas the keySet() method returns a list of all the HashMap keys. See the example below.

import java.util.HashMap;
import java.util.Map;

public class SimpleTesting{
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "Ten");
		map.put(100, "Hundred");
		map.put(1000, "Thousand");
		System.out.println(map.values());
		System.out.println(map.keySet());
	}
}

Output:

[Hundred, Thousand, Ten]
[100, 1000, 10]

The Biconsumer is an interface in Java that can be used to print HashMap elements using the lambda expression. See the example below.

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class SimpleTesting{
	public static void main(String[] args) {
		Map<Integer, String> map = new HashMap<>();
		map.put(10, "Ten");
		map.put(100, "Hundred");
		map.put(1000, "Thousand");
		BiConsumer<Integer, String> biconsumer = (key, val) -> 
		System.out.println(key + " = " + val);
		map.forEach(biconsumer);
	}
}

Output:

100 = Hundred
1000 = Thousand
10 = Ten

Related Article - Java HashMap