How to print hashmap in Java
In this post, you will learn how to print hashmap in the Java programming language.
Java Hashmap
Java HashMap class is found in the java.util package. Java HashMap class implements the Map interface, which allows us to store key and value pairs where keys should be unique. HashMap in Java is like the legacy Hashtable class, but it is not synchronized. It allows us to store the null elements as well, but there should be only one null key. The HashMap is non-synchronised and not thread safe. Java HashMap contain only unique keys. It is much faster in terms of retrieving data as compared to an array and linked-list.
Print hashmap in Java
There are different ways to print the HashMap in Java. Here, we have mentioned most of them-
Print HashMap in Java Using print Statement
The simple and easy way to print the hashmap in Java is by using the print statement.
import java.util.HashMap;
import java.util.Map;
public class DisplayHashMap{
public static void main(String[] args) {
Map<Integer, String> item = new HashMap<>();
item.put(5, "Notebook");
item.put(9, "Pen");
item.put(3, "Pencil");
item.put(2, "Eraser");
item.put(1, "Sharpener");
System.out.println(item);
}
}
Output of the above code:
{1=Sharpener, 2=Eraser, 3=Pencil, 5=Notebook, 9=Pen}
Print HashMap in Java using the keySet() method
The java.util.HashMap.keySet() method in Java is used to create a set out of the key elements contained in the hash map. We can use this to get a set of keys and then get the value using the get() method in the for loop.
import java.util.HashMap;
import java.util.Map;
public class DisplayHashMap{
public static void main(String[] args) {
Map<Integer, String> item = new HashMap<>();
item.put(5, "Notebook");
item.put(9, "Pen");
item.put(3, "Pencil");
item.put(2, "Eraser");
item.put(1, "Sharpener");
for (Integer key: item.keySet()){
System.out.println(key+ " = " + item.get(key));
}
}
}
Output of the above code:
1 = Sharpener
2 = Eraser
3 = Pencil
5 = Notebook
9 = Pen
Print HashMap in Java using the entrySet() Method
The entrySet() method of Map interface returns a set containing the map's entries, where the set entries are the objects of the Map.Entry type.
import java.util.HashMap;
import java.util.Map;
public class DisplayHashMap{
public static void main(String[] args) {
Map<Integer, String> item = new HashMap<>();
item.put(6, "Apple");
item.put(9, "Orange");
item.put(3, "Kiwi");
item.put(2, "Pineapple");
item.put(8, "Guava");
for( Map.Entry<Integer, String> entry : item.entrySet() ){
System.out.println( entry.getKey() + " = " + entry.getValue() );
}
}
}
Output of the above code:
2 = Pineapple
3 = Kiwi
6 = Apple
8 = Guava
9 = Orange
Print HashMap in Java using values() and keySet() method
Here, we have used the values() method with the keySet() method to print the HashMap. The values() method returns a list of all the values, whereas the keySet() method returns a list of all the HashMap keys.
import java.util.HashMap;
import java.util.Map;
public class DisplayHashMap{
public static void main(String[] args) {
Map<Integer, String> item = new HashMap<>();
item.put(6, "Apple");
item.put(9, "Orange");
item.put(3, "Kiwi");
item.put(2, "Pineapple");
item.put(8, "Guava");
System.out.println(item.values());
System.out.println(item.keySet());
}
}
Output of the above code:
[Pineapple, Kiwi, Apple, Guava, Orange]
[2, 3, 6, 8, 9]
Print HashMap in Java using the Biconsumer method
The BiConsumer Interface is a part of the java.util package. It has been introduced in Java 8, to implement functional programming in Java. This interface accepts two input arguments and returns no result. Here, we have used this to print HashMap elements using the lambda expression.
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class DisplayHashMap{
public static void main(String[] args) {
Map<Integer, String> item = new HashMap<>();
item.put(10, "Potato");
item.put(2, "Tomato");
item.put(1, "Onion");
item.put(3, "Chilli");
item.put(4, "Eggplant");
BiConsumer<Integer, String> biconsumer = (key, val) ->
System.out.println(key + " = " + val);
item.forEach(biconsumer);
}
}
Output of the above code:
1 = Onion
2 = Tomato
3 = Chilli
4 = Eggplant
10 = Potato
Print HashMap in Java using the Collections Class
The Java platform include a collection framework. Java Collection framework provides many interfaces(List) and classes(ArrayList, Vector, LinkedList). We can use the singletonList() static method of the collections framework to print the HashMap elements. This method returns a list representation of the HashMap.
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class DisplayHashMap{
public static void main(String[] args) {
Map<Integer, String> item = new HashMap<>();
item.put(10, "Potato");
item.put(2, "Tomato");
item.put(1, "Onion");
item.put(3, "Chilli");
item.put(4, "Eggplant");
System.out.println(Collections.singletonList(item));
}
}
Output of the above code:
[{1=Onion, 2=Tomato, 3=Chilli, 4=Eggplant, 10=Potato}]
Related Articles
Java string split multiple delimitersEnum with values in Java
Convert array to list in Java
Java random number between 1 and 100
Calculating percentage in Java
Multiplication table program in Java
Java dialogue box
Fibonacci series using recursion in Java
Java sum of array
Circular prime in Java
Vowel and Consonant program in Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Java program to check leap year
Java program to find factorial of a number