Java Hashtable

Java Hashtable class extends Dictionary class and implements Map interface.

HashMap stores key value pair.

HashTable is almost like HashMap with major two differences. First, it doesn’t allow null key and value on adding the same will throw NullPointerException. Second, HashTable is synchronized which means only one thread can access at the same time.

HashTable doesn’t maintain any order.

HashMap initial default capacity is 11 and loadFactor is 0.75.

Syntax:

Hashtable<key, val> ht = new Hashtable<key, val>();

Constructors:

Below are the constructors to create Hashtable object.

HashTable() → It will create HashTable with initial capacity 11 and load factor of 0.75

Hashtable<key, val> ht = new Hashtable<key, val>();

HashTable(int initialCapacity) → It will create HashTable with define initial capacity and default load factor of 0.75

Hashtable<key, val> ht = new Hashtable<key, val>(int initialCapacity);

HashTable(int size, float fillRatio) → It will create HashTable with define size and fill ratio

Hashtable<key, val> ht = new Hashtable<key, val>(int size, float fillRatio);

Example:

Below is the example to add and print Hashtable items. 

How HashTable Works Internally:

HashTable stores data in the form of bucket. HashTable consists of key and value pair. The Hashtable internally creates a hashcode for every key and stores the key value pair depending on the created hashcode.

As shown in below diagram where we have HashTable with multiple elements.

The first HashTable element has key as ‘0’ and after generating hashcode with the same ‘0’ key will save key value pair in bucket 0. Similarly, the second element has key as ‘1’ and after generating hashcode the same ‘1’ key will save key value pair in bucket 1. The same process will get repeat for all the elements in HashTable.

In java we can have same hashcode for different objects. In this scenario we can have single bucket having multiple key value pair elements called as collision.

Above HashTable bucket 0 have three different elements with the same hashcode. However, there keys and values are different.

How ‘Nitin’ will get fetch from HashTable?

It will perform below steps to find required key value pair:

  1. HashTable will create hashcode for passed key in our case it is ‘2’ inside get() method as parameter.
  2. It will fetch all elements from the matched hashcode bucket.
  3. loop through all the fetched elements from the specified bucket and compare key to get required result or key value pair.

Hashtable Methods:

Below are some of the methods from LinkedHashSet class.

put(): Method helps us to add an element in Hashtable.

replace(): Method will replace old value with new one using specified key.

remove(): Method helps us to remove an element from Hashtable.

get() :Method will return the specified value depending on the key passed as a parameter.

size(): Method returns the number of element in Hashtable Collection.

remove(): Method helps us to remove an element from Hashtable.

containsKey(): Method returns true if specified key is present in Hashtable collection else return false.

containsValue(): Method returns true if specified value is present in Hashtable collection else return false.

clear(): Method helps us to remove all the elements from Hashtable.

isEmpty(): Method returns true if number of elemetns is zero else return false.

OUTPUT:

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan