Various ways to iterate through Hashtable – 6 ways

In previous articles, we have discussed various ways to iterate through Map i.e.;

Various ways to iterate through Map

  • 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

 

Hashtable is legacy class and based on hashcode of keys where keys are unique and it is exactly same as HashMap with few differences with respect to following 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 insertion
  • Doesn’t allow Null value insertion

 

Hashtable being legacy class from Java 1.0 version, we can take advantage of Enumeration interface to iterate through Hashtable

 

Let us move forward and discuss all possible ways to iterate Hashtable in Java

Way 1: Using Enumeration interface in 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

 

 

Way 2: Get keys using keySet() method of Map 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

 

 

Way 3: Get keys using keySet() method of Map 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

 

 

Way 4: Get entries using entrySet() method of Map 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

 

 

Way 5: Get entries using entrySet() method of Map 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 and Iterator interface for iterating comes from Java 1.2 version, whereas enhanced for loop introduced in Java 1.5 version

 

 

Way 6: Using Iterable.forEach() in Java 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, 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

 

Happy Coding !!
Happy Learning !!