HashTable在Java中是如何工作的?

2023年 8月 28日 24.1k 0

HashTable在Java中是如何工作的?

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.

Java中Hashtable的工作原理

我们可以将Hashtable视为一个桶的数组,每个桶包含一个条目列表。一个条目由键和值组成。我们指定一个键和可以与该键关联的值。然后将键哈希化以生成哈希码,该哈希码进一步用作存储值在表中的索引。帮助从哈希码获取值位置的函数称为哈希函数。它总是返回一个称为哈希码的正整数值。多个对象在通过一个名为“equals()”的内置方法进行评估后可能获得相同的整数值。但是,相似的对象始终具有相同的哈希码。

Formula to allocate index

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。

Collision in Hashtable

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的语法

Hashtable 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()’方法来访问所有的键

  • 与键相关联的值。

Example 1

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 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

登录后复制

Example 2

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 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中是如何工作的?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论