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
Hashtablecontains values based on the key. Hashtablecontains unique elements.Hashtabledoesn’t allow null keys or values.- The initial default capacity of
Hashtableis 11, whereas theloadFactoris 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
Mainclass. - In line 4, we made a
mainfunction. - In line 6, we constructed a
HashTableusing the first constructor that created aHashTablewith a default initial capacity (11) and load factor (0.75) consisting ofIntegertype keys andStringtype values. - In lines 7 to 10, we inserted key-value pairs in the
Hashtableby using theHashtable.put()method. - In line 11, we displayed the
HashTableh1. - In line 13, we constructed a
HashTableusing the second constructor that created aHashTablewith a user defined initial capacity, i.e., 3 here, and default load factor (0.75) consisting ofIntegertype keys andStringtype values. - In lines 14 to 16, we inserted key-value pairs in the
Hashtableby using theHashtable.put()method. - In line 17, we displayed the
HashTableh2. - In line 19, we constructed a
HashTableusing the third constructor that created aHashTablewith a user defined initial capacity, i.e., 3 and load factor, i.e., 0.5 here, consisting ofIntegertype keys andStringtype values. - In lines 20 to 22, we inserted key-value pairs in the
HashTableby using theHashtable.put()method. - In line 23, we displayed the
HashTableh3. - In line 25, we declared a
HashmapwithIntegertype keys andStringtype values. - In lines 26 to 28, we inserted key-value pairs in the
HashMapby using theHashMap.put()method. - In line 29, we constructed a
HashTableusing the fourth constructor that created aHashTablewith the mapping of the createdHashMapthat is being passed. - In line 30, we displayed the
HashTableh4.