当前位置:网站首页>Detailed explanation of hash table
Detailed explanation of hash table
2022-07-27 11:43:00 【Shuke diary】
1、 Basic introduction of hash table
Hash table (Hash table, Also called hash table ) According to the key code value (Key value) Data structures that are accessed directly . in other words , It passes through Key values are mapped to tables To access records , To speed up the search . This The mapping function is called hash number, and the array storing records is called hash table
2、 Hashtable ( hash -Google) On the computer
1) Look at a real need ,google A computer problem of the company :
2) There's a company , When new employees come to report , Ask to add the employee's information to d, Gender , Age , address ), When you enter the employee's id when , Ask to find all the information of the employee
3) requirement : Don't use databases , Try to save memory , The faster the better => Hashtable ( hash )
3、 Hashtable ( hash -Google) Thinking analysis of computer problems

4、 Code implementation
package com.qf.hash;
import java.util.Scanner;
public class HashTabDemo {
public static void main(String[] args) {
HashTab hashTab=new HashTab(8);
boolean loop=true;
String sc="";
while (loop){
System.out.println("add Add employees ");
System.out.println("list Show employees ");
System.out.println("find Find employees ");
System.out.println("exit sign out ");
Scanner systemPut=new Scanner(System.in);
sc=systemPut.next();
switch (sc){
case "add":
System.out.println(" Please enter employee number :");
Scanner addNum=new Scanner(System.in);
int id = addNum.nextInt();
System.out.println(" Please enter the employee's name :");
Scanner nameScan=new Scanner(System.in);
String name = nameScan.next();
Emp emp=new Emp(id,name);
hashTab.add(emp);
break;
case "list":
hashTab.list();
break;
case "find":
System.out.println(" Please enter employee number :");
Scanner find=new Scanner(System.in);
int findId = find.nextInt();
hashTab.find(findId);
break;
case "exit":
loop=false;
System.out.println(" Exit loop ");
break;
default :
break;
}
}
}
}
class HashTab{
public EmpLinkedList[] linkedListArray;
public int maxSize;
public HashTab(int maxSize){
this.maxSize=maxSize;
linkedListArray=new EmpLinkedList[maxSize];
for (int i = 0; i < linkedListArray.length; i++) {
linkedListArray[i]=new EmpLinkedList();
}
}
public void add(Emp emp){
int id=emp.id;
int location=id%maxSize;
EmpLinkedList linkedList=linkedListArray[location];
linkedList.add(emp);
}
public void list(){
for (int i = 0; i < linkedListArray.length; i++) {
linkedListArray[i].list(i);
}
}
public void find(int id){
int location=id%maxSize;
EmpLinkedList linkedList=linkedListArray[location];
linkedList.find(id);
}
}
class EmpLinkedList{
public Emp head;
public EmpLinkedList(){
}
public void add(Emp emp){
if (head==null){
head=emp;
}else{
Emp temp=head;
while (true){
if (temp.next==null){
break;
}
temp=temp.next;
}
temp.next=emp;
}
}
public void list(int i){
if (head==null){
System.out.println(" The first "+i+" The length of the linked list is empty ");
}else {
Emp temp=head;
while (true){
System.out.println(" The first "+i+" Linked list employee id by :"+temp.id+" The name is :"+temp.name);
if (temp.next==null){
break;
}
temp=temp.next;
}
}
}
public void find(int id){
if (head==null){
System.out.println(" The length of the linked list is empty , Failed to find the employee ");
}else{
Emp temp=head;
boolean isFind=false;
while (true){
if (temp.id==id){
isFind=true;
System.out.println(" The employee's name is :"+temp.name);
break;
}
if (temp.next==null){
break;
}
temp=temp.next;
}
if (!isFind){
System.out.println(" Failed to find the employee ");
}
}
}
}
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 + '\'' +
'}';
}
}
边栏推荐
- Modelarts image classification and object detection
- Find the combination number acwing 886. find the combination number II
- 新版数据仓库的同步使用参考(新手向)
- 一些MathType常用快捷键
- 1. Introduction and basic use of flume
- 82.(cesium之家)cesium点在3d模型上运动
- 数据库 cli 工具 docker 镜像
- Several banks adjusted the redemption rules of cash management financial products: the confirmation time limit of redemption changed from "t+0" to "t+1"
- State compression DP acwing 91. shortest Hamilton path
- LeetCode-SQL练习题总结(MySQL实现)
猜你喜欢

MATLAB画带延时系统的伯德图

Codeforces round #664C

哈希表 详细讲解

Wilcoxon rank-sum 和 signed-rank

VSCode复制代码时去掉样式/语法高亮/代码高亮/黑色背景

N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei

Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan
新版数据仓库的同步使用参考(新手向)
![[unity entry program] creator kitfps: first person shooting 3D game](/img/2b/78b535973b2898f53752ceeb25ef01.png)
[unity entry program] creator kitfps: first person shooting 3D game

Find the combinatorial number acwing 889. 01 sequence satisfying the condition
随机推荐
The C programming language (2nd) -- Notes -- 1.6
箱型图介绍
Game theory acwing 894. Split Nim game
PWM的原理和PWM波的产生
【无标题】多模态模型 CLIP
Raw socket grabs packets, and packets on some ports cannot be caught
Maker Hongmeng application development training notes 03
局域网SDN硬核技术内幕 23 展望未来——RDMA(上)
w.r.t. ; i.e.;etc.;e.g.是什么意思
数据包传输:应用层-内核-硬件
Beyond Compare 3 下一个差异段/向下搜索箭头 找不到了
STM32编译出现error: L6235E: More than one section matches selector - cannot all be FIRST/L
Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan
本地虚拟机初始化脚本
origin如何作一张图中多张子图是柱状图(或其他图)
torch‘ has no attribute ‘inference_mode‘
Moveit2 - 5. Scenario Planning
Properties file
希腊字母读法
LeetCode 04: T26. 删除排序数组中的重复项(简单); 剑指 Offer 67. 把字符串转换成整数(中等); 面试题 01.08. 零矩阵 (简单)