Print HashMap in Java
-
Print
HashMap
Elements in Java -
Print
HashMap
Elements Using thekeySet()
Method in Java -
Print
HashMap
Elements UsingforEach()
Method in Java -
Print
HashMap
Elements UsingArrays.asList()
in Java -
Print
HashMap
Elements UsingCollections
Class in Java -
Print
HashMap
Elements Using theentrySet()
Method in Java -
Print
HashMap
Elements Usingvalues()
andkeySet()
Method in Java -
Print
HashMap
Elements UsingBiconsumer
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.
Print HashMap
Elements in Java
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}
Print HashMap
Elements Using the keySet()
Method in Java
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
Print HashMap
Elements Using forEach()
Method in Java
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());
});
}
}
Print HashMap
Elements Using Arrays.asList()
in Java
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}]
Print HashMap
Elements Using Collections
Class in Java
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}]
Print HashMap
Elements Using the entrySet()
Method in Java
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
Print HashMap
Elements Using values()
and keySet()
Method in Java
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]
Print HashMap
Elements Using Biconsumer
in Java
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
- Introduction to HashMap in Java
- Initialize HashMap in Java
- Collision in Hashmap in Java
- HashMap, HashSet and Hashtable in Java
- Sort a HashMap by Key in Java
- Sort HashMap in Java