How to Iterate Through HashTable in Java?
HashTable is an underlying data structure where the insertion order in HashTable is not preserved, and it is based on the hashcode of keys. Duplicates keys are not allowed, but values can be duplicated. Heterogeneous objects are allowed for both keys and values. Value null is not allowed for both key and value otherwise we will get RunTimeException saying NullPointerException. It implements serializable and cloneable interfaces but not RandomAccess. Every method inside it is synchronized and hence HashTable objects are thread-safe. HashTable is the best choice if our frequent operation is search operation.
Methods:
There are various ways by which we can iterate through the HashTable which are as follows:
- Using Enumeration Interface
- Using keySet() method of Map and Enhance for loop
- Using keySet() method of Map and Iterator Interface
- Using entrySet() method of Map and enhanced for loop
- Using entrySet() method of Map and Iterator interface
- Using Iterable.forEach() method from version Java 8
Now let us discuss the internal implementation of all methods one by one in detail to get a better understanding of iteration through HashTable
Method 1: Using Enumeration Interface
java.util.Enumeration interface is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable. In a forward direction only and not in the backward direction. This interface has been superseded by an iterator.
Java
import java.util.*;
import java.util.Enumeration;
class GFG {
public static void main(String[] args)
{
Hashtable<Integer, String> ht = new Hashtable<>();
ht.put( 1 , "Ram" );
ht.put( 2 , "Shyam" );
ht.put( 3 , "Bijay" );
ht.put( 4 , "Hritik" );
ht.put( 5 , "Piyush" );
Enumeration<Integer> e = ht.keys();
while (e.hasMoreElements()) {
int key = e.nextElement();
System.out.println( "Rank : " + key
+ "\t\t Name : "
+ ht.get(key));
}
}
}
|
Output
Rank : 5 Name : Piyush
Rank : 4 Name : Hritik
Rank : 3 Name : Bijay
Rank : 2 Name : Shyam
Rank : 1 Name : Ram
Method 2: Using keySet() method of Map and Enhance for loop
The java.util.HashMap.keySet() method in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys, or we can create a new set and store the key elements in them.
Java
import java.util.*;
import java.util.Set;
class GFG {
public static void main(String[] args)
{
Hashtable<Integer, String> ht = new Hashtable<>();
ht.put( 1 , "Java" );
ht.put( 2 , "Scala" );
ht.put( 3 , "Python" );
ht.put( 4 , "Pearl" );
ht.put( 5 , "R" );
Set<Integer> setOfKeys = ht.keySet();
for (Integer key : setOfKeys) {
System.out.println( "Rank : " + key
+ "\t\t Name : "
+ ht.get(key));
}
}
}
|
Output
Rank : 5 Name : R
Rank : 4 Name : Pearl
Rank : 3 Name : Python
Rank : 2 Name : Scala
Rank : 1 Name : Java
Method 3: Using keySet( ) method of Map and Iterator Interface
Again we will be using the same method as been implemented in the above example but here for iteration we will be using the Iterable interface
Java
import java.util.*;
import java.util.Iterator;
import java.util.Set;
class GFG {
public static void main(String[] args)
{
Hashtable<Integer, String> ht = new Hashtable<>();
ht.put( 1 , "Java" );
ht.put( 2 , "Scala" );
ht.put( 3 , "Python" );
ht.put( 4 , "Pearl" );
ht.put( 5 , "R" );
Set<Integer> setOfKeys = ht.keySet();
Iterator<Integer> itr = setOfKeys.iterator();
while (itr.hasNext()) {
int key = itr.next();
System.out.println( "Rank : " + key
+ "\t\t Name : "
+ ht.get(key));
}
}
}
|
Output
Rank : 5 Name : R
Rank : 4 Name : Pearl
Rank : 3 Name : Python
Rank : 2 Name : Scala
Rank : 1 Name : Java
Method 4: Using entrySet() method of Map and enhanced for loop
The java.util.HashMap.entrySet() method in Java is used to create a set out of the same elements contained in the hash map. It basically returns a set view of the hash map, or we can create a new set and store the map elements into them.
Java
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;
class GFG {
public static void main(String[] args)
{
Hashtable<Integer, String> ht = new Hashtable<>();
ht.put( 1 , "Java" );
ht.put( 2 , "Scala" );
ht.put( 3 , "Python" );
ht.put( 4 , "Pearl" );
ht.put( 5 , "R" );
Set<Entry<Integer, String> > entrySet
= ht.entrySet();
for (Entry<Integer, String> entry : entrySet) {
System.out.println( "Rank : " + entry.getKey()
+ "\t\t Name : "
+ entry.getValue());
}
}
}
|
Output
Rank : 5 Name : R
Rank : 4 Name : Pearl
Rank : 3 Name : Python
Rank : 2 Name : Scala
Rank : 1 Name : Java
Method 5: Using entrySet() method of Map and Iterator interface
Again we will be using the same method as been implemented in the above example but here for iteration we will be using the Iterable interface
Java
import java.util.*;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
class GFG {
public static void main(String[] args)
{
Hashtable<Integer, String> ht = new Hashtable<>();
ht.put( 1 , "Java" );
ht.put( 2 , "Scala" );
ht.put( 3 , "Python" );
ht.put( 4 , "Pearl" );
ht.put( 5 , "R" );
Set<Entry<Integer, String> > entrySet
= ht.entrySet();
Iterator<Entry<Integer, String> > itr
= entrySet.iterator();
while (itr.hasNext()) {
Entry<Integer, String> entry = itr.next();
System.out.println( "Rank : " + entry.getKey()
+ "\t\t Name : "
+ entry.getValue());
}
}
}
|
Output
Rank : 5 Name : R
Rank : 4 Name : Pearl
Rank : 3 Name : Python
Rank : 2 Name : Scala
Rank : 1 Name : Java
6. Using Iterable.forEach() method from version Java 8
With the coming of havoc new features in version, 8. It has been Quite a while since Java 8 released. With the release, they have improved some existing APIs and added few new features. One of them is forEach() method in java.lang.Iterable Interface.
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
Hashtable<Integer, String> ht = new Hashtable<>();
ht.put( 1 , "Java" );
ht.put( 2 , "Scala" );
ht.put( 3 , "Python" );
ht.put( 4 , "Ruby" );
ht.put( 5 , "R" );
ht.forEach((key, value)
-> System.out.println(
"Rank : " + key
+ "\t\t Name : " + value));
}
}
|
Output
Rank : 1 Name : Java
Rank : 2 Name : Scala
Rank : 3 Name : Python
Rank : 4 Name : Ruby
Rank : 5 Name : R
Last Updated :
24 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...