0

0

HashTable在Java中是如何工作的?

王林

王林

发布时间:2023-08-19 18:53:08

|

1201人浏览过

|

来源于tutorialspoint

转载

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。

EnablePPA中小学绩效考核系统2.0
EnablePPA中小学绩效考核系统2.0

无论从何种情形出发,在目前校长负责制的制度安排下,中小学校长作为学校的领导者、管理者和教育者,其管理水平对于学校发展的重要性都是不言而喻的。从这个角度看,建立科学的校长绩效评价体系以及拥有相对应的评估手段和工具,有利于教育行政机关针对校长的管理实践全过程及其结果进行测定与衡量,做出价值判断和评估,从而有利于强化学校教学管理,提升教学质量,并衍生带来校长转变管理观念,提升自身综合管理素质。

下载

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.

相关文章

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

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

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

14

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

7

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

6

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

20

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

106

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go语言教程-全程干货无废话
Go语言教程-全程干货无废话

共100课时 | 9.8万人学习

深入剖析redis教程
深入剖析redis教程

共55课时 | 8.1万人学习

Redis中文开发手册
Redis中文开发手册

共0课时 | 0人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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