![]() |
VOOZH | about |
The Hashtable class in Java is a legacy data structure that stores data in key-value pairs using a hash table. It is part of the Collections Framework and provides synchronized data access.
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
Hashtable Elements: {Two =2, Three =3, One =1}
In general, it's recommended to use the Map interface or one of its implementations (such as HashMap or ConcurrentHashMap) instead of the Hashtable class.
Note: While the Hashtable class still exists in Java and can still be used, it's generally recommended to use the Map interface or one of its implementations instead.
Hashtable implements Serializable, Cloneable, Map<K,V> interfaces and extends Dictionary<K,V>. The direct subclasses are Properties, UIDefaults.
In order to create a Hashtable, we need to import it from java.util.Hashtable. There are various ways in which we can create a Hashtable.
This creates an empty hashtable with the default load factor of 0.75 and an initial capacity is 11.
Hashtable<K, V> ht = new Hashtable<K, V>();
Mappings of ht1 : {3=three, 2=two, 1=one}
Mappings of ht2 : {6=six, 5=five, 4=four}
This creates a hash table that has an initial size specified by initialCapacity and the default load factor is 0.75.
Hashtable<K, V> ht = new Hashtable<K, V>(int initialCapacity);
Mappings of ht1 : {3=three, 2=two, 1=one}
Mappings of ht2 : {4=four, 6=six, 5=five}
This version creates a hash table that has an initial size specified by size and fill ratio specified by fillRatio. fill ratio: Basically, it determines how full a hash table can be before it is resized upward and its Value lies between 0.0 to 1.0.
Hashtable<K, V> ht = new Hashtable<K, V>(int size, float fillRatio);
Mappings of ht1 : {3=three, 2=two, 1=one}
Mappings of ht2 : {6=six, 5=five, 4=four}
This creates a hash table that is initialized with the elements in m.
Hashtable<K, V> ht = new Hashtable<K, V>(Map m);
Mappings of ht2 : {3=three, 2=two, 1=one}
Example:
Size of map is: 3
{vaibhav=20, vishal=10, sachin=30}
value for key "vishal" is: 10
Elements are added to a Hashtable using the put() method. It does not maintain insertion order and stores elements based on generated hash values for efficient access.
Mappings of ht1 : {3=Geeks, 2=For, 1=Geeks}
Mappings of ht2 : {3=Geeks, 2=For, 1=Geeks}
To update an element in a Hashtable, use the put() method with the same key; it replaces the existing value for that key.
Initial Map {3=Geeks, 2=Geeks, 1=Geeks}
Updated Map {3=Geeks, 2=For, 1=Geeks}
In order to remove an element from the Map, we can use the remove() method. This method takes the key value and removes the mapping for a key from this map if it is present in the map.
Initial map : {4=For, 3=Geeks, 2=For, 1=Geeks}
Updated map : {3=Geeks, 2=For, 1=Geeks}
To iterate the table, we can make use of an advanced for loop. Below is the example of iterating a hashtable.
vaibhav 20 vishal 10 sachin 30
Hashtable internally uses an array of buckets to store key-value pairs. It uses the hashCode() method to determine the index (bucket) where the data should be stored.
Method | Description |
|---|---|
| clear() | Clears this hashtable so that it contains no keys. |
| clone() | Creates a shallow copy of this hashtable. |
| Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). | |
| If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null. | |
| computeIfPresent(K key, BiFunction< super K,? super V, extends V> remappingFunction) | If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value. |
| contains(Object value) | Tests if some key maps into the specified value in this hashtable. |
| containsKey(Object key) | Tests if the specified object is a key in this hashtable. |
| containsValue(Object value) | Returns true if this hashtable maps one or more keys to this value. |
| elements() | Returns an enumeration of the values in this hashtable. |
| entrySet() | Returns a Set view of the mappings contained in this map. |
| equals(Object o) | Compares the specified Object with this Map for equality, as per the definition in the Map interface. |
| get(Object key) | Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. |
| hashCode() | Returns the hash code value for this Map as per the definition in the Map interface. |
| isEmpty() | Tests if this hashtable maps no keys to values. |
| keys() | Returns an enumeration of the keys in this hashtable. |
| keySet() | Returns a Set view of the keys contained in this map. |
| merge(K key, V value, BiFunction< super V, super V, extends V> remappingFunction) | If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. |
| put(K key, V value) | Maps the specified key to the specified value in this hashtable. |
| putAll(Map< extends K, extends V> t) | Copies all of the mappings from the specified map to this hashtable. |
| rehash() | Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently. |
| remove(Object key) | Removes the key (and its corresponding value) from this hashtable. |
| size() | Returns the number of keys in this hashtable. |
| toString() | Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space). |
| values() | Returns a Collection view of the values contained in this map. |
Method | Description |
|---|---|
| forEach(BiConsumer< super K, super V> action) | Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. |
| getOrDefault(Object key, V defaultValue) | Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key. |
| putIfAbsent(K key, V value) | If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value. |
remove(Object key, Object value) | Removes the entry for the specified key only if it is currently mapped to the specified value. |
| replace(K key, V value) | Replaces the entry for the specified key only if it is currently mapped to some value. |
| replace(K key, V oldValue, V newValue) | Replaces the entry for the specified key only if currently mapped to the specified value. |
| replaceAll(BiFunction< super K, super V, extends V> function) | Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. |