Hashtable in Java

The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values. Hashtable contains bucket (index) to store the key-value pair. The key’s hashcode is used to determine to which bucket the key-value pair should be mapped.

  • Hashtable is similar to HashMap, but it is synchronized whereas HashMap is not.
  • The object that is used as a key or as a value must not be null.
  • For successful storing and fetching of the object from the Hashtable, the object must implement the hashcode() and the equals() methods.
  • The hash function is used to get a bucket location from the key’s hashcode. A hash function always returns a number for an Object.

Let’s take the hashtable with & buckets :

Hashtable in Java

The performance of the hashtable is affected by its two parameters, i.e., initial capacity and load factor.

Initial Capacity-The number of buckets in the hashtable denotes its capacity. And the initial capacity is the capacity at the time hashtable is created. If the initial capacity is larger than the maximum number of entries the hashtable then, the rehash operation will not occur. The tradeoff between wasted space and requirement of rehash operation is controlled by initial capacity.

Load Factor-It is a measure of space acquired by the hashtable that is allowed to get before its capacity is automatically increased.

If we need to insert a large number of entries into the hashtable, it is better to create it with large capacity. It allows the values to be inserted more efficiently than letting it to perform automatic rehashing as required to grow the table.

Constructors of Hashtable

Constructor Description
Hashtable() It creates an empty hashtable with an initial default capacity of 11 and a load factor of 0.75.
Hashtable(int initialCapacity) It creates an empty hashtable with an initial capacity specified in the arguments with the default load factor of 0.75.
Hashtable(int initialCapacity, float loadFactor) It creates an empty hashtable with initial capacity and load factor specified in the argument.
Hashtable(Map t) It creates a new hashtable with the same mapping properties as given in the specified map in the arguments.

Methods of Hashtable

Modifier and Data type Methods Description
void Clear() It removes all the key-values from the hash table.
  forEach(BiConsumer action) It performs the specified action for each element in the map until all the elements have been processed or it throws an exception.
  replaceAll(BiFunction function) It replaces all the values of the entry with the result of invoking the specified function on that particular entry until all entries have been processed or the specified function throws an exception.
boolean contains(Object value) It tests the keymaps into the specified value in the hashtable or not.
  containsKey(Object key) It tests if the object specified in the argument is a key in the hashtable.
  containsValue(Object value) It returns true if the hashtable maps one or more keys to the specified value.
  equals(Object o) This method compares the argumented object with the map to check equality, according to the definition in the Map interface.
  isEmpty() This method tests if the hashtable didn’t map any keys to values
  remove(Object key, Object value) If the specified key is mapped to the specified value then only the entry gets removed by this method.
  replace(K key, V oldValue, V newValue) If the specified key is currently mapped to the specified value, the entry gets replace with the specified new value.
int hashCode() According to Map interface, it returns the hash code value of the map.
  size() It returns the number of keys in the hashtable.
String toString() It returns the string equivalent of the hashtable object in the form of a set of key-value pairs.
Object clone() It creates a shallow copy of the hashtable.
V compute(K key, BiFunction remappingFunction) It calculates the mapping for the specified key and its current mapped value or returns null if there is currently no mapping.
  computeIfAbsent(K key, Function mappingFunction) It attempts to compute the specified key’s value using the argumented mapping function if the key is not already associated with any value (or is mapped to null).
  computeIfPresent(K key, BiFunction remappingFunction) If the key specified in the argument is associated with any value, then it attempts to compute a new mapping with the key and its current mapped value.
  get(Object key) It returns the value that is associated with the specified key or returns null if the map does not contain mapping for the specified key.
  getOrDefault(Object key, V defaultValue) It returns the defaultValue specified in the argument if the map doesn’t contain a mapping for the specified key.
  Merge(K key, V value, Bifunction remapping) This method assigns the given non-null value to the specified key if the key is not already associated with any value or mapped to null.
  put(K key, V value) This method maps the specified value to the specified key.
Void putAll(Map t) It copies all the mappings from the map specified in the argument to the hashtable.
V putIfAbsent(K key, V value) This method assigns the given value to the specified key and return null if the key doesn’t contain any value, else returns the current value.
  remove(Object key) It removes the specified key-value pair from the hashtable.
  replace(K key, V value) It replaces the value of the specified key if it is currently mapped to some value.
protected void rehash() It increases the capacity of the hashtable and internally reorganizes it.
Enumeration <V> elements() Returns the list of the values available in the hashtable.
Ser<Map.Entry<K,V>> entrySet() It returns a “Set” view of the mapping contained in this map.
Enumeration<K> keys() It returns the list of the keys in the hashtable.
Set<K> keyset() This method gives a “Set” view of the keys contained in the map.
Collection<V> values() This method gives a “Collection” view of the values associated in the map.

Example to illustrate clear() and clone() method:

Output:

Example to illustrate forEach(BiConsumer action) method:

Output:

Example to demonstrate computeIfAbsent(Key, Function) method.

Output:

Example to illustrate the isEmpty() method

Output:

Example to demonstrate compute(Key, BiFunction) method.

Output:

Example to illustrate the put() and get() method.

Output:

Example to illustrate the containsValue() method.

Output:

Example to illustrate the containsKey() method

Output:

Example to illustrating Enumeration keys() method

Output:

Example to illustrating hashCode() method

Output:

Example to illustrating entrySet() method

Output:

Pin It on Pinterest

Share This