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.
我们可以将Hashtable视为一个桶的数组,每个桶包含一个条目列表。一个条目由键和值组成。我们指定一个键和可以与该键关联的值。然后将键哈希化以生成哈希码,该哈希码进一步用作存储值在表中的索引。帮助从哈希码获取值位置的函数称为哈希函数。它总是返回一个称为哈希码的正整数值。多个对象在通过一个名为“equals()”的内置方法进行评估后可能获得相同的整数值。但是,相似的对象始终具有相同的哈希码。
indexNumber = hashNumber % totalBuckets
Here, ‘%’ is the modulo operator that returns remainder
让我们来举一个例子,展示上述公式的使用 -
Q. 假设我们得到了一个名为XYZ的元素的哈希值为17,总数为 buckets available is 5. Then, find on which index number it will get stored?
解决方案 − 17 % 5 = 2 因此,它将获得索引号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<>();
第一步是导入 'java.util' 包,这样我们可以使用 Hashtable 类
Define an instance of the Hashtable class and append some objects into it using a 内置的名为‘put()’的方法。
现在,使用for-each循环,并在其中使用‘keySet()’方法来访问所有的键
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.
以上是HashTable在Java中是如何工作的?的详细内容。更多信息请关注PHP中文网其他相关文章!