This example shows how to iterate through Java Hashtable. This example also shows how to iterate through Hashtable keys, values, and entries using Iterator, Enumeration, for loop, while loop, and forEach method.
How to iterate through Hashtable in Java?
There are various ways in which we can iterate through hashtable keys, values, and entries as given below.
1. How to iterate over Hashtable keys?
We can get all the keys of the Hashtable using the keys
method. It returns an enumeration of all keys contained in the hash table object. Once we get that, we can iterate through hashtable keys using the hasMoreElements
and nextElement
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); //get enumeration of all keys using keys method Enumeration<Integer> keys = hashtable.keys(); //iterate using while loop while( keys.hasMoreElements() ){ System.out.println( keys.nextElement() ); } |
Output
1 2 3 |
3 2 1 |
We can also get a Set view of all the keys contained in the hashtable using the keySet
method. We can then get an Iterator for the Set and iterate using the hasNext
and next
methods as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); /* * Get the key set using the keySet method * and then iterator for it using the * iterator method */ Iterator<Integer> itr = hashtable.keySet().iterator(); //iterate while( itr.hasNext() ){ System.out.println( itr.next() ); } |
2. How to iterate over Hashtable values?
We can get all the values contained in the Hashtable object using the elements
method. It returns an Enumeration of all the values of the hash table object. Once we get the enumeration, we can iterate through the values using the hasMoreElements
and nextElement
methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); //get enumeration of all the values Enumeration<String> values = hashtable.elements(); //iterate through values while( values.hasMoreElements() ){ System.out.println( values.nextElement() ); } |
Output
1 2 3 |
Three Two One |
We can also use the Hashtable values
method that returns a Collection view of all the values contained in the hashtable object. Once we get the values collection object, we can get an iterator and iterate through it as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); /* * Get the Collection view of all the values * of the hashtable and then get the iterator */ Iterator<String> itr = hashtable.values().iterator(); while( itr.hasNext() ){ System.out.println( itr.next() ); } |
Output
1 2 3 |
Three Two One |
3. How to iterate over Hashtable entries (key-value mappings)?
We can get all the entries of the Hashtable using the entrySet
method. Once we get a Set view of all the mappings of the hashtable object, we can iterate through the entries using below given ways.
Using Iterator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); //get the entry set using the entrySet method Set<Map.Entry<Integer, String>> entries = hashtable.entrySet(); //get an iterator Iterator<Map.Entry<Integer, String>> itr = entries.iterator(); //iterate using the iterator Map.Entry<Integer, String> entry = null; while( itr.hasNext() ){ entry = itr.next(); System.out.println( entry.getKey() + "=>" + entry.getValue() ); } |
Using enhanced for loop
We can also use the enhanced for loop as given below.
1 2 3 4 5 6 7 |
//get the entry set using the entrySet method Set<Map.Entry<Integer, String>> entries = hashtable.entrySet(); //iterate using the for loop for( Map.Entry<Integer, String> entry : entries ){ System.out.println( entry.getKey() + "=>" + entry.getValue()); } |
Output
1 2 3 |
3=>Three 2=>Two 1=>One |
Using forEach
If you are using Java version 8 or later, you can also use the forEach
method to iterate through hashtable entries as given below.
1 2 3 4 5 6 7 |
//get the entry set using the entrySet method Set<Map.Entry<Integer, String>> entries = hashtable.entrySet(); //iterate using the forEach entries.forEach( entry ->{ System.out.println(entry.getKey() + "=>" + entry.getValue()); } ); |
Output
1 2 3 |
3=>Three 2=>Two 1=>One |
This example is a part of the Hashtable in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Hashtable Documentation