Java Hashtable Class



Introduction

The Java Hashtable class implements a hashtable, which maps keys to values.Following are the important points about Hashtable −

  • In this any non-null object can be used as a key or as a value.

  • 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

Following is the declaration for java.util.Hashtable class −

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

Class constructors

Sr.No. Constructor & Description
1

Hashtable()

This constructs a new, empty hashtable with a default initial capacity (11) and load factor (0.75).

2

Hashtable(int initialCapacity)

This constructs a new, empty hashtable with the specified initial capacity and default load factor (0.75).

3

Hashtable(int initialCapacity, float loadFactor)

This constructs a new, empty hashtable with the specified initial capacity and the specified load factor.

4

Hashtable(Hashtable<? extends K,? extends V> t)

This constructs a new hashtable with the same mappings as the given Hashtable.

Class methods

Sr.No. Method & Description
1 void clear()

This method clears this hashtable so that it contains no keys.

2 Object clone()

This method 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 hashtable 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)

This method tests if some key maps into the specified value in this hashtable.

7 boolean containsKey(Object key)

This method tests if the specified object is a key in this hashtable.

8 boolean containsValue(Object value)

This method returns true if this hashtable maps one or more keys to this value.

9 Enumeration<V> elements()

This method returns an enumeration of the values in this hashtable.

10 Set<Hashtable.Entry<K,V>> entrySet()

This method returns a Set view of the mappings contained in this hashtable.

11 boolean equals(Object o)

This method compares the specified Object with this Hashtable for equality, as per the definition in the Hashtable interface.

12 V get(Object key)

This method returns the value to which the specified key is mapped, or null if this hashtable contains no mapping for the key.

13 int hashCode()

This method returns the hash code value for this Hashtable as per the definition in the Hashtable interface.

14 boolean isEmpty()

This method tests if this hashtable maps no keys to values.

15 Enumeration<K> keys()

This method returns an enumeration of the keys in this hashtable.

16 Set<K> keySet()

This method returns a Set view of the keys contained in this hashtable.

17 V put(K key, V value)

This method maps the specified key to the specified value in this hashtable.

18 void putAll(Hashtable<? extends K,? extends V> t)

This method copies all of the mappings from the specified hashtable to this hashtable.

19 V remove(Object key)

This method removes the key (and its corresponding value) from this hashtable.

20 int size()

This method returns the number of keys in this hashtable.

21 String toString()

This method 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).

22 Collection<V> values()

This method returns a Collection view of the values contained in this hashtable.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object

Adding a Mapping to a HashTable of Integer, Integer Pair Example

The following example shows the usage of Java Hashtable put() method to put few values in a Hashtable. We've created a Hashtable object of Integer,Integer pairs. Then few entries are added using put() method and then table is printed.

package com.tutorialspoint;

import java.util.Hashtable;

public class HashtableDemo {
   public static void main(String args[]) {
      
      // create hash table
      Hashtable<Integer,Integer> hashtable = new Hashtable<>();

      // populate hash table
      hashtable.put(1, 1);
      hashtable.put(2, 2);
      hashtable.put(3, 3); 

      System.out.println("Hashtable elements: " + hashtable);
   }    
}

Let us compile and run the above program, this will produce the following result.

Hashtable elements: {3=3, 2=2, 1=1}
Advertisements