Facebook Page Twitter Page LinkedIn Page
× Java Utility Library


Java Hashtable Class

Java.util package provides a Hashtable class which implements hash table and maps keys to value. 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. If many entries are to be made into a Hashtable, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.

Class declaration

The declaration of java.util.Hashtable class is:

public class Hashtable<K,V>
  extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, Serializable

Here, K and V are the type of key and value respectively maintained by the container.

Class Constructors

S.NConstructors & Description
1. Hashtable()
Creates an empty Hashtable with a default initial capacity (11) and load factor (0.75).
2. Hashtable(int initialCapacity)
Creates an empty Hashtable with the specified initial capacity and default load factor (0.75).
3. Hashtable(int initialCapacity, float loadFactor)
Creates an empty Hashtable with the specified initial capacity and the specified load factor.
4. Hashtable(Map<? extends K,? extends V> t)
Creates a new Hashtable with the same mappings as the given Map.

java.util.Hashtable Methods

The java.util.Hashtable class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. void clear()
Clears the hashtable so that it contains no keys.
2. Object clone()
Creates a shallow copy of this hashtable.
3. V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
4. V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
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.
5. V 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.
6. boolean contains(Object value)
Returns true if some key maps into the specified value in this hashtable.
7. boolean containsKey(Object key)
Returns true if the specified object is a key in this hashtable.
8. boolean containsValue(Object value)
Returns true if this hashtable maps one or more keys to this value.
9. Enumeration<V> elements()
Returns an enumeration of the values in this hashtable.
10. Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map.
11. boolean equals(Object obj)
Compares the specified Object with this Map for equality, as per the definition in the Map interface.
12. void 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.
13. V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
14. V 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.
15. int hashCode()
Returns the hash code value for this Map as per the definition in the Map interface.
16. boolean isEmpty()
Checks whether the hashtable maps no keys to values.
17. Enumeration<K> keys()
Returns an enumeration of the keys in this hashtable.
18. Set<K> keySet()
Returns a Set view of the keys contained in this map.
19. V 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.
20. V put(K key, V value)
Maps the specified key to the specified value in the hashtable.
21. void putAll(Map<? extends K,? extends V> t)
Copies all of the mappings from the specified map to this hashtable.
22. V 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.
23. protected void rehash()
Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently.
24. V remove(Object key)
Removes the key (and its corresponding value) from this hashtable.
25. boolean remove(Object key, Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.
26. V replace(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value.
27. boolean replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.
28. void 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.
29. int size()
Returns the number of keys in the hashtable.
30. String toString()
Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by ", " (comma and space).
31. Collection<V> values()
Returns a Collection view of the values contained in this hashtable.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.Dictionary<K,V>