What are ways to create a HashTable in Java?

In this shot, we will discuss the different ways to create a HashTable in Java.

The HashTable class implements a hashtable, which maps keys to values. A Hashtable is an array of a list.

  • A Hashtable contains values based on the key.
  • Hashtable contains unique elements.
  • Hashtable doesn’t allow null keys or values.
  • The initial default capacity of Hashtable is 11, whereas the loadFactor is 0.75.

Constructors

The four constructors of the HashTable class are listed below:

HashTable()

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

HashTable(int capacity)

This constructs a new, empty hashtable with the defined initial capacity and default load factor of 0.75.

HashTable(int capacity, float loadFactor)

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

HashTable(Map<? extends K,? extends V> m)

This constructs a new hashtable with the same mappings as the defined in the HashMap.

Code

Let’s look at the code snippet.

import java.util.*;
class Main
{
public static void main(String[] args)
{
Hashtable<Integer, String> h1 = new Hashtable<Integer, String>();
h1.put(1, "Let's");
h1.put(5, "see");
h1.put(2, "Hashtable");
h1.put(9, "in java.");
System.out.println("The Hashtable h1 is: "+h1);
Hashtable<Integer, String> h2 = new Hashtable<Integer, String>(3);
h2.put(1, "Let's");
h2.put(55, "see");
h2.put(20, "Hashtable");
System.out.println("The Hashtable h2 is: "+h2);
Hashtable<Integer, String> h3 = new Hashtable<Integer, String>(3,0.5f);
h3.put(11, "Let's");
h3.put(15, "see");
h3.put(14, "Hashtable");
System.out.println("The Hashtable h3 is: "+h3);
Map<Integer, String> map = new HashMap<>();
map.put(17, "Let's");
map.put(62, "see");
map.put(33, "Hashtable");
Hashtable<Integer, String> h4 = new Hashtable<Integer, String>(map);
System.out.println("The Hashtable h4 is: "+h4);
}
}

Explanation

  • In line 1, we imported the required package.
  • In line 2, we made a Main class.
  • In line 4, we made a main function.
  • In line 6, we constructed a HashTable using the first constructor that created a HashTable with a default initial capacity (11) and load factor (0.75) consisting of Integer type keys and String type values.
  • In lines 7 to 10, we inserted key-value pairs in the Hashtable by using the Hashtable.put() method.
  • In line 11, we displayed the HashTable h1.
  • In line 13, we constructed a HashTable using the second constructor that created a HashTable with a user defined initial capacity, i.e., 3 here, and default load factor (0.75) consisting of Integer type keys and String type values.
  • In lines 14 to 16, we inserted key-value pairs in the Hashtable by using the Hashtable.put() method.
  • In line 17, we displayed the HashTable h2.
  • In line 19, we constructed a HashTable using the third constructor that created a HashTable with a user defined initial capacity, i.e., 3 and load factor, i.e., 0.5 here, consisting of Integer type keys and String type values.
  • In lines 20 to 22, we inserted key-value pairs in the HashTable by using the Hashtable.put() method.
  • In line 23, we displayed the HashTable h3.
  • In line 25, we declared a Hashmap with Integer type keys and String type values.
  • In lines 26 to 28, we inserted key-value pairs in the HashMap by using the HashMap.put() method.
  • In line 29, we constructed a HashTable using the fourth constructor that created a HashTable with the mapping of the created HashMap that is being passed.
  • In line 30, we displayed the HashTable h4.

Free Resources