How to Iterate Over Values of Hashtable in Java


In this example, let us see how to iterate through the values contained in Java Hashtable object.

Source Code

package com.beginner.examples;

import java.util.Enumeration;
import java.util.Hashtable;

public class IterateValuesOfHashtable {

	public static void main(String[] args) {
		
		//create Hashtable.
		Hashtable hashtable = new Hashtable();

		hashtable.put("1","Alice");
		hashtable.put("2","Bob");
		hashtable.put("3","jack");
		
		/*
		 * Using  elements() method of Hashtable to get Enumeration of values contained in Hashtable.
		 */
		Enumeration e = hashtable.elements();
		while(e.hasMoreElements()) {
			System.out.println(e.nextElement());
		}
	}

}

Output:

jack
Bob
Alice

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments