How to Get Hashtable Values in Java


This example will explain how to get a Collection of values contained in Hashtable with values method of java Hashtable class.

Source Code

package com.beginner.examples;

import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;

public class GetCollectionOfValuesFromHashtable {

	public static void main(String[] args) {
		
		//create Hashtable. 
		Hashtable hashtable = new Hashtable();
		
		hashtable.put("1","Alice");
		hashtable.put("2","Bob");
		hashtable.put("3","Jack");
		
		/*
		 * get Collection of values contained in Hashtable using values() method of Hashtable.
		 */
		Collection collection = hashtable.values();
		Iterator iterator = collection.iterator();
		System.out.println("The collection contains :");
		while(iterator.hasNext()) {
			System.out.println(iterator.next());
		}
		

	}

}

Output:

The collection contains :
Jack
Bob
Alice

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments