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

相关专题

更多
Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

37

2026.01.14

php与html混编教程大全
php与html混编教程大全

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

19

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

37

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.13

PHP缓存策略教程大全
PHP缓存策略教程大全

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

6

2026.01.13

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

交互式图表和动态图表教程汇总
交互式图表和动态图表教程汇总

本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

45

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

9

2026.01.13

热门下载

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

精品课程

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

共100课时 | 9.6万人学习

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

共55课时 | 8万人学习

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

共0课时 | 0人学习

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

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