The Hashtable class is a part of the Java Collection Framework that stores its element in key-value pairs in a hash table. The Key is an object that can be used to fetch and receive value associated with it. There exist a few similarities between a Hashtable and HashMapclass but Hash table is synchronized. Also, its keys must be associated with values, they could not be null. This article aims to explain how Hash table works internally in Java.
We can think of a Hashtable as an array of buckets, each bucket containing a list of entries. An entry consists of a key and a value. We specify a key and the values that can be associated with that key. The key is then hashed to generate a hash code which is further used as an index in the table where the value is stored. The function that helps in getting the position of the value from the hash code is called a hash function. It always returns a positive integer value called hash code. Multiple objects may obtain the same integer value after evaluation through a built-in method called "equals()". However, similar objects always have the same hash code.
indexNumber = hashNumber % totalBuckets
Here, ‘%’ is the modulo operator that returns remainder
Let us give an example to show the use of the above formula -
Q. Suppose we get an element named XYZ with a hash value of 17 and a total of buckets available is 5. Then, find on which index number it will get stored?
Solution − 17 % 5 = 2 Therefore, it will get index number 2.
As discussed earlier, multiple objects might get same hashcode which leads to a situation called collision. It occurs when two or more keys have the same hash value and are mapped to the same bucket resulting in slow performance. However, it does not create any functional confusion.
Hashtable<TypeOfKey, TypeOfValue> nameOfTable = new Hashtable<>();
The first step is to import the 'java.util' package so that we can use the Hashtable class
Define an instance of the Hashtable class and append some objects into it using a Built-in method named 'put()'.
Now, use a for-each loop and use the ‘keySet()’ method inside it to access all the keys
The following example illustrates how we can implement a Hashtable in Java.
import java.util.*; public class Table { public static void main(String[] args) { Hashtable<String, Integer> workers = new Hashtable<>(); // Adding elements in the workers table workers.put("Vaibhav", 4000); workers.put("Ansh", 3000); workers.put("Vivek", 1500); workers.put("Aman", 2000); workers.put("Tapas", 2500); // printing details workers table System.out.println("Elements in the given table: "); for (String unKey : workers.keySet()) { System.out.println("Name: " + unKey + ", Salary: " + workers.get(unKey)); } } }
Elements in the given table: Name: Aman, Salary: 2000 Name: Ansh, Salary: 3000 Name: Tapas, Salary: 2500 Name: Vivek, Salary: 1500 Name: Vaibhav, Salary: 4000
In the following example, we will retrieve the values of a Hashtable by using the in-built method ‘get()’. This method accepts a key and returns the corresponding value.
import java.util.*; public class Table { public static void main(String[] args) { Hashtable<String, Integer> workers = new Hashtable<>(); // Adding elements in the workers table workers.put("Vaibhav", 4000); workers.put("Ansh", 3000); workers.put("Vivek", 1500); workers.put("Aman", 2000); workers.put("Tapas", 2500); // printing details workers table one by one System.out.println("Value stored at key Ansh: " + workers.get("Ansh")); System.out.println("Value stored at key Vivek: " + workers.get("Vivek")); System.out.println("Value stored at key Aman: " + workers.get("Aman")); } }
Value stored at key Ansh: 3000 Value stored at key Vivek: 1500 Value stored at key Aman: 2000
We started this article by defining the Hashtable class and in the next section, we explained how it works internally through an example. Later, we discussed the practical implementation of Hashtable through Java example programs.
The above is the detailed content of How does HashTable work in Java?. For more information, please follow other related articles on the PHP Chinese website!