Java HashTable Class
This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. Hashtable is similar to HashMap except it is synchronized. There are few more differences between HashMap and Hashtable class, you can read them in detail here Difference between HashMap and Hashtable.
In this journal, we will see how to create a Hashtable, how to populate its entries and then we will learn how to display its key-value pairs using Enumeration.
HashTable Example in Java
import java.util.Hashtable; import java.util.Enumeration; public class HashtableExample { public static void main(String[] args) { Enumeration names; String key; // Creating a Hashtable Hashtable<String, String> hashtable = new Hashtable<String, String>(); // Adding Key and Value pairs to Hashtable hashtable.put("Key1","Developers"); hashtable.put("Key2","Journal"); hashtable.put("Key3","Java"); hashtable.put("Key4","Android"); hashtable.put("Key5","Kotlin"); names = hashtable.keys(); while(names.hasMoreElements()) { key = (String) names.nextElement(); System.out.println("Key: " +key+ " & Value: " + hashtable.get(key)); } } }
Output
Key: Key4 & Value: Android Key: Key3 & Value: Java Key: Key2 & Value: Journal Key: Key1 & Value: Developers Key: Key5 & Value: Kotlin
Methods of HashTable class:
- void clear(): Removes all the key-value mappings from Hashtable and makes it empty. Clears this hashtable so that it contains no keys.
- Object clone(): Creates a shallow copy of this hashtable. All the structure of the hashtable itself is copied, but the keys and values are not cloned. This is a relatively expensive operation.
- boolean contains(Object value): Tests if some key maps into the specified value in this hashtable. This operation is more expensive than the containsKey method. Note that this method is identical in functionality to containsValue, (which is part of the Map interface in the collections framework).
- boolean isEmpty(): Tests if this hashtable maps no keys to values.
- Enumeration keys(): Returns an enumeration of the keys contained in the hash table.
- Object put(Object key, Object value): Maps the specified key to the specified value in this hashtable.
- void rehash(): Increases the size of the hash table and rehashes all of its keys.
- Object remove(Object key): Removes the key (and its corresponding value) from this hashtable.
- int size(): Returns the number of key-value mappings present in Hashtable.
- String toString(): Returns the string equivalent of a hash table.
- boolean containsKey(Object key): Tests if the specified object is a key in this hashtable.
- boolean containsValue(Object value): Tests if the specified object is a value in this hashtable. Returns true if some value equal to value exists within the hash table. Returns false if the value isn’t found.
- Enumeration elements(): Returns an enumeration of the values contained in the hash table.
- Object get(Object key): Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.