This example shows how to print Hashtable in Java. This example also shows how to print Hashtable keys, values, or entries in the console using an iterator, for loop, and forEach method.
How to print Hashtable in Java?
The simplest way to print the hashtable in the console or to the log file is by using the toString
method. The Hashtable class has overridden the toString
method from the Object class. It returns a string representation of all entries stored in the hashtable enclosed by “{” and “}” and separated by “, “.
1 2 3 4 5 6 7 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); System.out.println( hashtable ); |
Output
1 |
{3=Three, 2=Two, 1=One} |
There are various ways using which we can print hashtable keys, values or entries as given below.
How to print all the keys of the Hashtable?
1. Using the keys method
Get enumeration of all keys of the Hashtable and print them one by one.
1 2 3 4 5 |
Enumeration<Integer> keys = hashtable.keys(); while(keys.hasMoreElements()){ System.out.println(keys.nextElement()); } |
Output
1 2 3 |
3 2 1 |
2. Using the keySet method
We can get the Set view of all the keys contained in the hashtable using the keySet
method. We can then iterate and print them using an iterator.
1 2 3 4 5 |
Iterator<Integer> itr = hashtable.keySet().iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } |
Output
1 2 3 |
3 2 1 |
How to print all the values of the Hashtable?
1. Using the elements method
The elements
method returns an enumeration of all the values of the Hashtable object.
1 2 3 4 5 |
Enumeration<String> values = hashtable.elements(); while( values.hasMoreElements() ){ System.out.println( values.nextElement() ); } |
Output
1 2 3 |
Three Two One |
2. Using the values method
The values
method returns a Collection view of all values of the hash table object.
1 2 3 4 5 |
Iterator<String> itr = hashtable.values().iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } |
Output
1 2 3 |
Three Two One |
How to print all the entries or mappings of the Hashtable?
We can get a Set view of all the entries or mappings of hashtable using the entrySet
method. We can then iterate through hashtable entries using below given ways.
1. Using an Iterator
1 2 3 4 5 6 7 8 |
Iterator<Map.Entry<Integer, String>> itr = hashtable.entrySet().iterator(); Map.Entry<Integer, String> entry = null; while(itr.hasNext()){ entry = itr.next(); System.out.println( entry.getKey() + "->" + entry.getValue() ); } |
Output
1 2 3 |
3->Three 2->Two 1->One |
2. Using the enhanced for loop
1 2 3 4 5 |
Set<Map.Entry<Integer, String>> entries = hashtable.entrySet(); for(Map.Entry<Integer, String> entry : entries ){ System.out.println( entry.getKey() + "->" + entry.getValue() ); } |
Output
1 2 3 |
3->Three 2->Two 1->One |
3. Using the forEach
We can also use the forEach
method to print hashtable mappings (for Java version 8 and later).
1 2 3 |
hashtable.entrySet().forEach( entry -> { System.out.println( entry.getKey() + "->" + entry.getValue() ); }); |
Output
1 2 3 |
3->Three 2->Two 1->One |
How to print Hashtable of custom class objects?
When we print hashtable containing custom class objects in console, it does not produce useful information as shown in below example.
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 |
import java.util.Hashtable; class User{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } } public class PrintHashtableExample { public static void main(String[] args) { Hashtable<Integer, User> htUsers = new Hashtable<Integer, User>(); htUsers.put(1, new User(1)); htUsers.put(2, new User(2)); System.out.println(htUsers); } } |
Output
1 |
{2=com.javacodeexamples.collections.hashtable.User@15db9742, 1=com.javacodeexamples.collections.hashtable.User@6d06d69c} |
Since the User class has not overridden the toString
method, the toString
method inherited from the Object class is called that prints object information in “className@objectHashCode” format.
Let’s override the toString
method in the User 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 |
import java.util.Hashtable; class User{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "User -> " + getId(); } } public class PrintHashtableExample { public static void main(String[] args) { Hashtable<Integer, User> htUsers = new Hashtable<Integer, User>(); htUsers.put(1, new User(1)); htUsers.put(2, new User(2)); System.out.println(htUsers); } } |
Output
1 |
{2=User -> 2, 1=User -> 1} |
This example is a part of the Java Hashtable Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Hashtable Documentation