In previous articles, we have discussed various ways to iterate through Map and in this article we will learn different ways to iterate through Hashtable
1. Hashtable :
- Hashtable is legacy class and based on hashcode of keys where keys are unique
- It’s exactly same as HashMap with few differences as mentioned in the succeeding points
- All methods of Hashtable are Synchronized methods
- Performance is slightly on the lower side when compared with HashMap, as only one thread is allowed to operate on Hashtable after acquiring lock
- Doesn’t allow Null Key and Null Value insertion
Hashtable being legacy class from Java 1.0 version, we can take advantage of Enumeration interface to iterate through Hashtable
2. Ways to iterate through Hashtable :
- Using legacy Enumeration interface introduced in Java 1.0 version
- Using keySet() method and for-each loop
- Using keySet() method and Iterator interface
- Using entrySet() method and for-each loop
- Using entrySet() method and Iterator interface
- Using forEach() in Java 8
- Read different ways to iterate Map Entry
- Read how to iterate Map using Iterable.forEach() in Java 8
Let’s move forward and discuss all possible ways to iterate over Hashtable in Java
2.1 Using Enumeration from Java 1.0 version :
IteratingHashtableUsingEnumeration.java
package in.bench.resources.hashtable.iteration.ways;
import java.util.Enumeration;
import java.util.Hashtable;
public class IteratingHashtableUsingEnumeration {
public static void main(String[] args) {
// creating Hashtable object of type <String, String>
Hashtable<String, String> ht = new Hashtable<String, String>();
// adding key-value pairs to Hashtable object
ht.put("Russia", "Vladimir Putin");
ht.put("France", "Napoléon Bonaparte");
ht.put("Zimbabwe", "Robert Mugabe");
ht.put("Cuba", "Fidel Castro");
ht.put("Germany", "Adolf Hitler");
ht.put("Italy", "Benito Mussolini");
// Way 1: Using Enumeration interface from Java 1.0 version
System.out.println("Way 1: Using Enumeration interface"
+ " from Java 1.0 version\n");
// get keys() from Hashtable and iterate
Enumeration<String> enumeration = ht.keys();
// iterate using enumeration object
while(enumeration.hasMoreElements()) {
String key = enumeration.nextElement();
System.out.println("Country : " + key
+ "\t\t President : " + ht.get(key));
}
}
}
Output:
Way 1: Using Enumeration interface from Java 1.0 version
Country : Cuba President : Fidel Castro
Country : France President : Napoléon Bonaparte
Country : Germany President : Adolf Hitler
Country : Italy President : Benito Mussolini
Country : Russia President : Vladimir Putin
Country : Zimbabwe President : Robert Mugabe
2.2 Get keys using Map.keySet() and iterate using enhanced for loop :
IteratingHashtableUsingKeySetAndForLoop.java
package in.bench.resources.hashtable.iteration.ways;
import java.util.Hashtable;
import java.util.Set;
public class IteratingHashtableUsingKeySetAndForLoop {
public static void main(String[] args) {
// creating Hashtable object of type <String, String>
Hashtable<String, String> ht = new Hashtable<String, String>();
// adding key-value pairs to Hashtable object
ht.put("Russia", "Vladimir Putin");
ht.put("France", "Napoléon Bonaparte");
ht.put("Zimbabwe", "Robert Mugabe");
ht.put("Cuba", "Fidel Castro");
ht.put("Germany", "Adolf Hitler");
ht.put("Italy", "Benito Mussolini");
// Way 2: Get keySet() and Iterate using for-each loop
System.out.println("Way 2: Get keySet() and"
+ " Iterate using for-each loop\n");
// getting keySet() into Set
Set<String> setOfCountries = ht.keySet();
// for-each loop
for(String key : setOfCountries) {
System.out.println("Country : " + key
+ "\t\t President : " + ht.get(key));
}
}
}
Output:
Way 2: Get keySet() and Iterate using for-each loop
Country : Cuba President : Fidel Castro
Country : France President : Napoléon Bonaparte
Country : Germany President : Adolf Hitler
Country : Italy President : Benito Mussolini
Country : Russia President : Vladimir Putin
Country : Zimbabwe President : Robert Mugabe
2.3 Get keys using Map.keySet() and iterate using Iterator interface :
IteratingHashtableUsingKeySetAndIterator.java
package in.bench.resources.hashtable.iteration.ways;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
public class IteratingHashtableUsingKeySetAndIterator {
public static void main(String[] args) {
// creating Hashtable object of type <String, String>
Hashtable<String, String> ht = new Hashtable<String, String>();
// adding key-value pairs to Hashtable object
ht.put("Russia", "Vladimir Putin");
ht.put("France", "Napoléon Bonaparte");
ht.put("Zimbabwe", "Robert Mugabe");
ht.put("Cuba", "Fidel Castro");
ht.put("Germany", "Adolf Hitler");
ht.put("Italy", "Benito Mussolini");
// Way 3: Get keySet() and Iterate using Iterator interface
System.out.println("Way 3: Get keySet() and"
+ " Iterate using Iterator interface\n");
// getting keySet() into Set
Set<String> setOfCountries = ht.keySet();
// Collection Iterator
Iterator<String> iterator = setOfCountries.iterator();
while(iterator.hasNext()) {
String key = iterator.next();
System.out.println("Country : " + key
+ "\t\t President : " + ht.get(key));
}
}
}
Output:
Way 3: Get keySet() and Iterate using Iterator interface
Country : Cuba President : Fidel Castro
Country : France President : Napoléon Bonaparte
Country : Germany President : Adolf Hitler
Country : Italy President : Benito Mussolini
Country : Russia President : Vladimir Putin
Country : Zimbabwe President : Robert Mugabe
2.4 Get entries using Map.entrySet() and iterate using enhanced for loop :
IteratingHashtableUsingEntrySetAndForLoop.java
package in.bench.resources.hashtable.iteration.ways;
import java.util.Hashtable;
import java.util.Map.Entry;
import java.util.Set;
public class IteratingHashtableUsingEntrySetAndForLoop {
public static void main(String[] args) {
// creating Hashtable object of type <String, String>
Hashtable<String, String> ht = new Hashtable<String, String>();
// adding key-value pairs to Hashtable object
ht.put("Russia", "Vladimir Putin");
ht.put("France", "Napoléon Bonaparte");
ht.put("Zimbabwe", "Robert Mugabe");
ht.put("Cuba", "Fidel Castro");
ht.put("Germany", "Adolf Hitler");
ht.put("Italy", "Benito Mussolini");
// Way 4: Get entrySet() and Iterate using for-each loop
System.out.println("Way 4: Get entrySet() and"
+ " Iterate using for-each loop\n");
// getting entrySet() into Set
Set<Entry<String, String>> entrySet = ht.entrySet();
// for-each loop
for(Entry<String, String> entry1 : entrySet) {
System.out.println("Country : " + entry1.getKey()
+ "\t\t President : " + entry1.getValue());
}
}
}
Output:
Way 4: Get entrySet() and Iterate using for-each loop
Country : Cuba President : Fidel Castro
Country : France President : Napoléon Bonaparte
Country : Germany President : Adolf Hitler
Country : Italy President : Benito Mussolini
Country : Russia President : Vladimir Putin
Country : Zimbabwe President : Robert Mugabe
2.5 Get entries using Map.entrySet() and iterate using Iterator interface :
IteratingHashtableUsingEntrySetAndIterator.java
package in.bench.resources.hashtable.iteration.ways;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class IteratingHashtableUsingEntrySetAndIterator {
public static void main(String[] args) {
// creating Hashtable object of type <String, String>
Hashtable<String, String> ht = new Hashtable<String, String>();
// adding key-value pairs to Hashtable object
ht.put("Russia", "Vladimir Putin");
ht.put("France", "Napoléon Bonaparte");
ht.put("Zimbabwe", "Robert Mugabe");
ht.put("Cuba", "Fidel Castro");
ht.put("Germany", "Adolf Hitler");
ht.put("Italy", "Benito Mussolini");
// Way 5: Get entrySet() and Iterate using Iterator interface
System.out.println("Way 5: Get entrySet() and"
+ " Iterate using Iterator interface\n");
// getting entrySet() into Set
Set<Entry<String, String>> entrySet = ht.entrySet();
// Collection Iterator
Iterator<Entry<String, String>> iterator = entrySet.iterator();
while(iterator.hasNext()) {
Entry<String, String> entry2 = iterator.next();
System.out.println("Country : " + entry2.getKey()
+ "\t\t President : " + entry2.getValue());
}
}
}
Output:
Way 5: Get entrySet() and Iterate using Iterator interface
Country : Cuba President : Fidel Castro
Country : France President : Napoléon Bonaparte
Country : Germany President : Adolf Hitler
Country : Italy President : Benito Mussolini
Country : Russia President : Vladimir Putin
Country : Zimbabwe President : Robert Mugabe
Note:
- keySet() and entrySet() methods of Map from Java 1.2 version
- Iterator interface for iterating comes from Java 1.2 version
- whereas enhanced for loop introduced in Java 1.5 version
2.6 Using Iterable.forEach() from Java 1.8 version :
IteratingHashtableUsingJava8ForEach.java
package in.bench.resources.hashtable.iteration.ways;
import java.util.Hashtable;
public class IteratingHashtableUsingJava8ForEach {
public static void main(String[] args) {
// creating Hashtable object of type <String, String>
Hashtable<String, String> ht = new Hashtable<String, String>();
// adding key-value pairs to Hashtable object
ht.put("Russia", "Vladimir Putin");
ht.put("France", "Napoléon Bonaparte");
ht.put("Zimbabwe", "Robert Mugabe");
ht.put("Cuba", "Fidel Castro");
ht.put("Germany", "Adolf Hitler");
ht.put("Italy", "Benito Mussolini");
// Way 6: Using Iterable.forEach() in Java 8 version
System.out.println("Way 6: Using "
+ "Iterable.forEach() in Java 8 version\n");
// Iterating Map/Hashtable using forEach() in Java 8
ht.forEach((key, value)->System.out.println("Country : " + key
+ "\t\t" + "President : " + value));
}
}
Output:
Way 6: Using Iterable.forEach() in Java 8 version
Country : Zimbabwe President : Robert Mugabe
Country : Russia President : Vladimir Putin
Country : Italy President : Benito Mussolini
Country : Germany President : Adolf Hitler
Country : Cuba President : Fidel Castro
Country : France President : Napoléon Bonaparte
From above example, we can say Hashtable
- allows only unique keys to be stored
- null is NOT allowed for both keys and values
- while iterating, keys are retrieved in random-order
Related Articles :
- Java 8 – Iterating Map using forEach()
- Java 8 – Iterating HashMap in 8 ways
- Java 8 – Various ways to iterate through Hashtable – 6 ways
- Java 8 – Various ways to iterate over HashMap of ArrayList
References :
- https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
Happy Coding !!
Happy Learning !!