1.1、哈希表基本介绍
![image-20210321091033085](https://i-blog.csdnimg.cn/blog_migrate/8c6b7e454198bd785defd4265fc59c8b.png)
![image-20210321091121264](https://i-blog.csdnimg.cn/blog_migrate/cb04d0421be58470074d1c3772bc5d56.png)
1.2、思路分析
![image-20210321103218928](https://i-blog.csdnimg.cn/blog_migrate/3d77ad5702e778baa7a4118a9d10a347.png)
![image-20210321091743722](https://i-blog.csdnimg.cn/blog_migrate/25645bbf45f314529e21f4badb07c24c.png)
1.3、代码实现
package com.qingchengxiaoye.hashtab;
import java.util.Scanner;
public class HashTabDemo {
public static void main(String[] args) {
HashTab hashTab = new HashTab(7);
String key = "";
Scanner scanner = new Scanner(System.in);
while(true) {
while(true) {
System.out.println("add: 添加雇员");
System.out.println("list: 显示雇员");
System.out.println("find: 查找雇员");
System.out.println("exit: 退出系统");
key = scanner.next();
int id;
switch(key.hashCode()) {
case 96417:
if (key.equals("add")) {
System.out.println("输入id");
id = scanner.nextInt();
System.out.println("输入名字");
String name = scanner.next();
Emp emp = new Emp(id, name);
hashTab.add(emp);
}
break;
case 3127582:
if (key.equals("exit")) {
scanner.close();
System.exit(0);
}
break;
case 3143097:
if (key.equals("find")) {
System.out.println("请输入要查找的id");
id = scanner.nextInt();
hashTab.findEmpById(id);
}
break;
case 3322014:
if (key.equals("list")) {
hashTab.list();
}
}
}
}
}
}
class Emp {
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
public int id;
public String name;
public Emp next;
}
class EmpLinkedList {
private Emp head;
public void add(Emp emp) {
if (this.head == null) {
this.head = emp;
} else {
Emp curEmp;
for (curEmp = this.head; curEmp.next != null; curEmp = curEmp.next) {
}
curEmp.next = emp;
}
}
public void list(int no) {
if (head == null) {
System.out.println("第 " + (no + 1) + " 链表为空");
} else {
System.out.print("第 " + (no + 1) + " 链表的信息为");
Emp curEmp = this.head;
while (true) {
System.out.printf(" => id=%d name=%s\t", curEmp.id, curEmp.name);
if (curEmp.next == null) {
System.out.println();
return;
}
curEmp = curEmp.next;
}
}
}
public Emp findEmpById(int id){
if (this.head==null){
System.out.println("链表为空");
return null;
}
Emp curEmp=head;
while (true){
if (curEmp.id==id){
break;
}
if (curEmp.next==null){
curEmp=null;
break;
}
curEmp=curEmp.next;
}
return curEmp;
}
}
class HashTab {
private EmpLinkedList[] empLinkedListArray;
private int size;
public HashTab(int size) {
this.size = size;
this.empLinkedListArray = new EmpLinkedList[size];
for (int i = 0; i < size; i++) {
this.empLinkedListArray[i] = new EmpLinkedList();
}
}
public void add(Emp emp) {
int empLinkedListNo = this.hashFun(emp.id);
this.empLinkedListArray[empLinkedListNo].add(emp);
}
public void list() {
for (int i = 0; i < this.size; ++i) {
this.empLinkedListArray[i].list(i);
}
}
public void findEmpById(int id){
int empLinkedListNo = hashFun(id);
Emp emp = empLinkedListArray[empLinkedListNo].findEmpById(id);
if (emp!=null){
System.out.printf("在第%d条链表中找到雇员 id=%d\n",(empLinkedListNo+1),id);
}else {
System.out.println("在哈希表中没有找到该雇员");
}
}
public int hashFun(int id) {
return id % this.size;
}
}