什么是哈希表
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章主要介绍"什么是哈希表",在日常操作中,相信很多人在什么是哈希表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"什么是哈希表"的疑惑有所帮助!接下来,请跟着小编一
千家信息网最后更新 2024年09月22日什么是哈希表
这篇文章主要介绍"什么是哈希表",在日常操作中,相信很多人在什么是哈希表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"什么是哈希表"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
基本介绍
散列表(Hash Table,也叫哈希表),是根据关键码值(Key Value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中的一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表
Google上机题
有一个公司,当有新员工来报到时,要求将该员工的信息加入(id,性别,年龄,地址….),当输入该员工的id时,要求查找该员工的所有信息。
要求:不使用数据库,尽量节省内存,速度越快越好。
package com.xie.hashtable; import java.util.Scanner; public class HashTableDemo { public static void main(String[] args) { //创建一个HashTab HashTab hashTab = new HashTab(7); String key = ""; Scanner scanner = new Scanner(System.in); while (true) { System.out.println("add:添加雇员"); System.out.println("list:显示雇员"); System.out.println("find:查找雇员"); System.out.println("delete:删除雇员"); System.out.println("exit:退出程序"); key = scanner.next(); switch (key) { case "add": System.out.println("输入id"); int id = scanner.nextInt(); System.out.println("输入name"); String name = scanner.next(); Emp emp = new Emp(id, name); hashTab.add(emp); break; case "list": hashTab.list(); break; case "find": System.out.println("请输入编号"); int no = scanner.nextInt(); hashTab.findEmpById(no); break; case "delete": System.out.println("请输入编号"); int deleteNo = scanner.nextInt(); hashTab.deleteEmpById(deleteNo); break; case "exit": scanner.close(); System.exit(0); break; default: break; } } } } //创建哈希表,管理多条链表 class HashTab { private int size; private EmpLinkedList[] empLinkedListArray; public HashTab(int size) { this.size = size; empLinkedListArray = new EmpLinkedList[size]; for (int i = 0; i < size; i++) { empLinkedListArray[i] = new EmpLinkedList(); } } //添加雇员 public void add(Emp emp) { //根据员工的id,得到该员工应当添加到哪条链表 int empLinkedListNo = hashFun(emp.id); //将emp添加到对应的链表 empLinkedListArray[empLinkedListNo].add(emp); } //查找员工 public Emp findEmpById(int id) { int no = hashFun(id); Emp empById = empLinkedListArray[no].findEmpById(id); if (empById == null) { System.out.println("不存在id=" + id + "元素"); return null; } else { System.out.println("存在id=" + id + ",name=" + empById.name); return empById; } } //删除雇员 public void deleteEmpById(int id) { int no = hashFun(id); empLinkedListArray[no].deleteEmp(id); } //遍历哈希表 public void list() { for (int i = 0; i < size; i++) { empLinkedListArray[i].list(i); } } //编写散列函数,使用简单的取模法 private int hashFun(int id) { return id % size; } } //表示雇员 class Emp { public int id; public String name; public Emp next; public Emp(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "Emp{" + "id=" + id + ", name='" + name + '\'' + '}'; } } //表示链表 class EmpLinkedList { //头指针 private Emp head; //添加雇员到链表 //说明: //1.当添加雇员时,id时自增的,即id分配总是从小到大,因此我们将该雇员直接加到本链表的最后即可 public void add(Emp emp) { //如果是添加第一个雇员 if (head == null) { head = emp; } else { Emp curr = head; while (true) { if (curr.next == null) { break; } curr = curr.next; } curr.next = emp; } } //遍历 public void list(int no) { if (head == null) { System.out.println("第" + (no + 1) + "链表为空"); return; } System.out.print("第" + (no + 1) + "链表信息为:"); Emp curr = head; while (true) { if (curr != null) { System.out.printf("=> id=%d,name=%s\t", curr.id, curr.name); curr = curr.next; } else { break; } } System.out.println(); } //根据id查找雇员 public Emp findEmpById(int id) { if (head == null) { System.out.println("链表为空"); return null; } Emp temp = head; while (temp != null) { if (temp.id == id) { return temp; } temp = temp.next; } return null; } //根据id删除雇员 public void deleteEmp(int id) { if (head == null) { System.out.println("没有id=" + id + "的雇员"); return; } Emp curr = head; boolean flag = false; while (true) { if (curr == null) { break; } if (curr.next.id == id) { flag = true; break; } curr = curr.next; } if (flag) { curr.next = curr.next.next; } else { System.out.println("没有id=" + id + "的雇员"); } } }
到此,关于"什么是哈希表"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
雇员
哈希
员工
输入
学习
信息
关键
函数
数据
更多
帮助
实用
接下来
也就是
也就是说
位置
元素
公司
内存
地址
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
mc远古版本手机版服务器
利用网络技术支持课堂教学改革
sql实验六数据库安全
海波网络技术有限公司
远程linux达梦数据库使用
计算机实用网络技术论文
网络安全等级评定中心
定西服务器核酸
顺通互联网科技有限公司
济南市服务器包装价格
数据库登录权限怎么设置
数据库两段锁
正规网络安全工作方案供应商
微什么的app软件开发
北京通用软件开发厂家价格
芯片服务器好不好
嘉定区网络技术咨询诚信服务
传统wifi网络技术
php网站数据库如何修改
杨浦区软件开发要多少钱
本地服务器有nas和oss吗
辽宁省网络安全大讲堂体会
电子商务数据库技术课程报告
网络安全马斯克
超频服务器的价格
视频服务器出错
网络安全保卫辅助
php服务器集群
法院开展网络安全集中学习
网络安全论文15000字