首页 > Java > java教程 > 正文

HashTable在Java中是如何工作的?

王林
发布: 2023-08-19 18:53:08
转载
1196人浏览过

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

立即学习Java免费学习笔记(深入)”;

让我们来举一个例子,展示上述公式的使用 -

Q. 假设我们得到了一个名为XYZ的元素的哈希值为17,总数为 buckets available is 5. Then, find on which index number it will get stored?

解决方案 − 17 % 5 = 2 因此,它将获得索引号2。

如知AI笔记
如知AI笔记

如知笔记——支持markdown的在线笔记,支持ai智能写作、AI搜索,支持DeepseekR1满血大模型

如知AI笔记 27
查看详情 如知AI笔记

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

  • 与键相关联的值。

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<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
登录后复制

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<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中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号