Different ways to iterate Hashtable in Java

1. Using Iterator – Iterating Hashtable using an iterator.

package iteratehashtable;
import java.util.*;
public class Example1 {
public static void main(String[] args) {
	Map<Integer, String> hashtableObject = new Hashtable<>();
	 
	hashtableObject.put(2, "ram");
	hashtableObject.put(3, "mohan");
	hashtableObject.put(4, "Bangalore");
	hashtableObject.put(5, "Rakesh");

	// we will iterate hashtable using Iterator
	Iterator<Map.Entry<Integer, String>> iterator = hashtableObject
			.entrySet().iterator();

	while (iterator.hasNext()) {
		Map.Entry<Integer, String> entry = iterator.next();
		System.out.println("Key is = " + entry.getKey() + ", Value is = "
				+ entry.getValue());
	}
}
}

Output is –

Key is = 4, Value is = Bangalore
Key is = 3, Value is = mohan
Key is = 2, Value is = ram

2. Using for each loop -Iterating Hashtable using for each loop.

package iteratehashtable;

import java.util.*;

 
public class Example2 {
	public static void main(String[] args) {
		Map<Integer, String> hashMapObj = new Hashtable<>();
 
		hashMapObj.put(2, "ram");
		hashMapObj.put(5, "mohan");
		hashMapObj.put(3, "sohan");
 
		Set<Map.Entry<Integer, String>> entrySet = hashMapObj.entrySet();
 
		for (Map.Entry<Integer, String> entry : entrySet) {
			System.out.println("Key is : " + entry.getKey() + "   Value is : "
					+ entry.getValue());
		}
	}
}

Output is –

Key is : 2 Value is : ram
Key is : 3 Value is : sohan
Key is : 5 Value is : mohan

3. Using Lambda expression – Iterating HashMap using a lambda expression.

package iteratehashtable;


import java.util.*;

 
public class Example3 {
	public static void main(String[] args) {
		Map<Integer, String> hashtableObj = new Hashtable<>();
 
		hashtableObj.put(2, "ram");
		hashtableObj.put(5, "mohan");
		hashtableObj.put(3, "sohan");
		hashtableObj.put(9, "rohit");
 
		hashtableObj.forEach((k, v) -> {
			System.out.println("Key is : " + k + "   Value is : " + v);
		});
 
	}
}

Output is –

Key is : 2 Value is : ram
Key is : 3 Value is : sohan
Key is : 5 Value is : mohan
Key is : 9 Value is : rohit

4. Using Java 8 stream() method –

package iteratehashtable;

import java.util.*;

 
public class Example4 {
	public static void main(String[] args) {
		Map<Integer, String> hashtableObj = new Hashtable<>();
 
		hashtableObj.put(2, "ram");
		hashtableObj.put(5, "mohan");
		hashtableObj.put(3, "sohan");
		hashtableObj.put(9, "rohit");
 
		Set<Map.Entry<Integer, String>> setReference= hashtableObj.entrySet();
 
		setReference.stream().forEach
		(mapEntry-> System.out.println("Key is : "+mapEntry.getKey()+"   Value is : "+mapEntry.getValue()));
	}
}

Output is –

Key is : 9 Value is : rohit
Key is : 5 Value is : mohan
Key is : 3 Value is : sohan
Key is : 2 Value is : ram

5. Using the keySet() and Iterator – 

package iteratehashtable;

import java.util.*;

 
public class Example5 {
	public static void main(String[] args) {
		Map<Integer, String> hashtableObject = new Hashtable<>();
 
		hashtableObject.put(2, "ram");
		hashtableObject.put(5, "mohan");
		hashtableObject.put(3, "sohan");
		hashtableObject.put(9, "rohit");
 
		Iterator iterator = hashtableObject.keySet().iterator();
 
		while (iterator.hasNext()) {
			Integer key = (Integer) iterator.next();
 
			System.out.println("Key is : " + key + "   Value is : " + hashtableObject.get(key));
		}
	}
}

 Output is –

Key is : 9 Value is : rohit
Key is : 5 Value is : mohan
Key is : 3 Value is : sohan
Key is : 2 Value is : ram

6. Using keySet() and for each loop – 

package iteratehashtable;


import java.util.Hashtable;
import java.util.Map;
 
public class Example6 {
	public static void main(String[] args) {
		Map<Integer, String> hashtableObj = new Hashtable<>();
 
		hashtableObj.put(2, "ram");
		hashtableObj.put(5, "mohan");
		hashtableObj.put(3, "sohan");
		hashtableObj.put(9, "rohit");
 
		for (Integer key : hashtableObj.keySet()) {
			System.out.println("Key is : " + key + "   Value is : " + hashtableObj.get(key));
		}
	}
}

Output is –

Key is : 9 Value is : rohit
Key is : 5 Value is : mohan
Key is : 3 Value is : sohan
Key is : 2 Value is : ram