This example shows how to print HashMap in Java. The example also shows how to print all keys, all values, and all key-value pairs of HashMap using different ways.
How to print HashMap in Java?
The AbstractMap class, the parent class of the HashMap class, has overridden the toString
method which returns a string representation of the map. All key-value pairs are enclosed in { and } and separated by a comma (,). The iterator of the entry set returns the order of the key-value pairs.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class PrintHashMapExample { public static void main(String[] args) { HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); System.out.println( hMapNumbers ); } } |
Output
1 |
{1=One, 2=Two, 3=Three} |
How to print all keys and values of HashMap using entrySet?
If you do not want the default formatting done by the toString
method, you can get the entry set from the HashMap and print all key-value pairs one by one using the for loop as given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); /* * Entry.getKey method returns the key and * Entry.getValue returns the value of the HashMap entry. */ for( Map.Entry<Integer, String> entry : hMapNumbers.entrySet() ){ System.out.println( entry.getKey() + " => " + entry.getValue() ); } |
Output
1 2 3 |
1 => One 2 => Two 3 => Three |
Java 8 and above
If you are using Java version 8 and above, you can use the below given code to print all keys and values of HashMap.
1 2 3 4 5 6 7 8 |
HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); hMapNumbers.entrySet().forEach( entry -> { System.out.println( entry.getKey() + " => " + entry.getValue() ); }); |
Output
1 2 3 |
1 => One 2 => Two 3 => Three |
How to print all the keys of HashMap?
The keySet
method of the HashMap class returns a Set view containing all the keys of the HashMap.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); /* * Get the set containing all keys using * keySet method and iterate it using the * for loop to print all keys */ Set<Integer> setKeys = hMapNumbers.keySet(); for(Integer key : setKeys){ System.out.println( key ); } |
Output
1 2 3 |
1 2 3 |
You can also use the System.out.println statement instead of using the for loop if you do not want to change the output format.
1 2 |
//will print all keys in format [key1, key2...] System.out.println( setKeys ); |
Output
1 |
[1, 2, 3] |
How to print all the values of the HashMap?
The values
method of the HashMap returns a Collection view containing all the values contained in the HashMap.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); /* * Get all the values from the HashMap using the * values method and then use for loop to * print all values */ Collection<String> values = hMapNumbers.values(); for(String value : values){ System.out.println( value ); } |
Output
1 2 3 |
One Two Three |
You can also use System.out.println statement instead of using the for loop if you do not want to change the output format.
1 2 |
//will output all values in format [value1, value2...] System.out.println( values ); |
Output
1 |
[One, Two, Three] |
How to print HashMap containing custom class object as keys or values?
In all of the above examples, we printed Integer and String objects using the System.out.println statement. They were printed fine because both of these classes have overridden the toString
method. Let’s try to print HashMap containing custom class objects.
We have put objects of Emp class as values in the HashMap in below given example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Emp{ private Integer id; private String name; public Emp(Integer id, String name){ this.id = id; this.name = name; } } public class PrintHashMapExample { public static void main(String[] args) { HashMap<Integer, Emp> hMapNumbers = new HashMap<Integer, Emp>(); hMapNumbers.put(1, new Emp(1, "John")); hMapNumbers.put(2, new Emp(2, "Alex")); System.out.println( hMapNumbers ); } } |
Output
1 |
{1=com.javacodeexamples.collections.hashmap.Emp@15db9742, 2=com.javacodeexamples.collections.hashmap.Emp@6d06d69c} |
As you can see from the output, the keys were printed fine but the values were not. It is because our Emp class has not overridden the toString
method so it inherited the method from the Object class.
The toString
method of the Object class returns the class name of the object, followed by @, followed by the hexadecimal hash code of the object. The format is not readable and hence it is suggested for all the subclasses to override the toString
method to produce informative text.
Let’s override the toString
method in the Emp class and try again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class Emp{ private Integer id; private String name; public Emp(Integer id, String name){ this.id = id; this.name = name; } /* * This method will be called whenever we print * Emp object using System.out.println statement. */ public String toString(){ return "[" + this.id + ", " + this.name + "]"; } } public class PrintHashMapExample { public static void main(String[] args) { HashMap<Integer, Emp> hMapNumbers = new HashMap<Integer, Emp>(); hMapNumbers.put(1, new Emp(1, "John")); hMapNumbers.put(2, new Emp(2, "Alex")); System.out.println( hMapNumbers ); } } |
Output
1 |
{1=[1, John], 2=[2, Alex]} |
Now our program printed the HashMap containing custom Emp objects as the values correctly.
Tip: Always override the toString
method for the objects used as either keys or values of the HashMap.
This tutorial is a part of the Java HashMap tutorial.
Please let me know your views in the comments section below.
I really get trouble with printing value of the object. Thank you It was really helpfull