Java Program to Read Elements using Enumeration in Hashtable
Last Updated :
22 Jul, 2021
The enumeration in java is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable(like Stack, Vector, HashTable, etc.) in a forward direction only and not in the backward direction.
HashTable is a class The hash table class implements a Map, which maps keys to values. It stores the key/value pair in the hash table. In this data structure we specify an object that is used as a key, and the value we want to associate with that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table. HashMap doesn’t provide any Enumeration, while HashTable provides not fail-fast Enumeration. The hierarchy of the hash table is as follows:

Syntax:
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
Parameters:
- K: Keys inserted.
- V: Values assigned to the keys.
In order to create HashTable, import hash Table from java.util.Hashtable where, K, V are of data types like integer, string, float, etc.
Syntax: Creating hash Table
Hashtable<K, V> ht = new Hashtable<K, V>();
Implementation: College Student Information
Example
Java
// Java Program to read elements
// using enumeration in hashtable
// Importing enumeration class
import java.util.Enumeration;
// Importing hash table
import java.util.Hashtable;
// Class
public class GFG {
// Main driver method
public static void main(String a[])
{
// Creating hash table
Hashtable<String, String> hm
= new Hashtable<String, String>();
// Add key-value pair to Hashtable
// Custom inputs
hm.put("Name", "Bahubali");
hm.put("College", "Amarnath");
hm.put("Department", "Vedics");
// enum
Enumeration<String> keys = hm.keys();
// Condition check whether element(K,V) is present
// using hasMoreElements()
while (keys.hasMoreElements()) {
String key = keys.nextElement();
// Print corresponding key-value pair
System.out.println("Value of " + key
+ " is: " + hm.get(key));
}
System.out.println();
// Creating a new Hashtable
Hashtable<String, String> hm1
= new Hashtable<String, String>();
// Adding key-value pair to Hashtable
// Custom inputs
hm1.put("Name", "Ravaan");
hm1.put("College", "SriLanka");
hm1.put("Department", "CS");
// Enum
Enumeration<String> keys1 = hm1.keys();
// Condition check whether element(K,V) is present
// using hasMoreElements()
while (keys1.hasMoreElements()) {
String key = keys1.nextElement();
// Print corresponding key-value pair
System.out.println("Value of " + key
+ " is: " + hm1.get(key));
}
System.out.println();
// Creating a new Hashtable
Hashtable<String, String> hm2
= new Hashtable<String, String>();
// Adding key-value pair to Hashtable
// Custom inputs
hm2.put("Name", "Kattappa");
hm2.put("College", "Beardo");
hm2.put("Department", "War");
/// enum
Enumeration<String> keys2 = hm2.keys();
// Condition check whether element(K,V) is present
// using hasMoreElements()
while (keys2.hasMoreElements()) {
String key = keys2.nextElement();
// Print corresponding key-value pair
System.out.println("Value of " + key
+ " is: " + hm2.get(key));
}
}
}
OutputValue of Name is: Bahubali
Value of College is: Amarnath
Value of Department is: Vedics
Value of Name is: Ravaan
Value of College is: SriLanka
Value of Department is: CS
Value of Name is: Kattappa
Value of College is: Beardo
Value of Department is: War
Similar Reads
How to Use Enumeration to Display Elements of Hashtable in Java? Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. Now here we can get the keys and
2 min read
Java Program to Iterate Vector using Enumeration The Vector class implements a growable array of objects. It is available in java.util package. It implements the List interface. The Enumeration interface defines the methods by which you can traverse the elements in a collection of objects. Now in order to add elements Vector Syntax: public class V
2 min read
Java Program to Implement HashTable API The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. To implement Hashtable API f
4 min read
How to Convert a HashTable to Other Collections Types in Java? In Java, a Hashtable is a data structure that stores the data in the form of key and value pairs. And each key is mapped to a specific value. It implements the Map interface. HashTable provides a simple way to access the data using the unique keys. In this article, we will learn how to convert a Has
3 min read
Java Program to Get First or Last Elements from HashSet The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. There is no guarantee made for the iteration order of the set which means that the class does not guarantee the constant order of elements over time. This class permits the null element. The
3 min read
Java Program to Create ArrayList From Enumeration Enumerations serve the purpose of representing a group of named constants in a programming language. Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay
2 min read
Java Program to Iterate LinkedHashSet Elements The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements
3 min read
How to Create a Synchronized HashTable in Java? In Java, a synchronized HashTable is achieved by wrapping a regular HashTable with the Collection.synchronizedMap( ) method. This wrapper ensures that each method of the Map interface is synchronized, making the HashTable thread-safe. Syntax:Map<KeyType, ValueType> synchronizedHashTable = Coll
3 min read
Java Program to Implement Hash Tables with Double Hashing Double hashing is a technique in an open addressing scheme. and there is the ordinary hash function. In an open addressing scheme, the actual hash function is taking the ordinary hash function when its space is not empty then it will perform another hash function to get some space to insert. Double
10 min read
Java Program to Get Elements By Index from LinkedHashSet LinkedHashSet is a pre-defined class in Java that is similar to HashSet. Unlike HashSet In LinkedHashSet insertion order is preserved. In order to get element by Index from LinkedHashSet in Java, we have multiple ways.Illustration:Input : 2, 3, 4, 2, 7;Processing : index = 4;Output : Element at inde
4 min read