第十章 哈希表
10.1 哈希表基本介绍
散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表
10.2 google题目
有一个公司,当有新员工来报道时,要求该员工的信息加入(id,性别,年龄···),当输入该员工的 id 时,要求查找到该员工的所有信息
要求
不使用数据库,速度越快越好 => 哈希表(散列)
10.3 代码演示
package com.crisp.HashTable;
import java.util.Scanner;
public class HashTableDemo {
public static void main(String[] args) {
//创建一个哈希表
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.printf("输入 id:");
int id = scanner.nextInt();
System.out.printf("输入名字:");
String name = scanner.next();
//创建雇员
Emp emp = new Emp(id, name);
hashTab.add(emp);
break;
case "list":
hashTab.list();
break;
case "find":
System.out.printf("请输入雇员id:");
int findId = scanner.nextInt();
hashTab.findEmpById(findId);
break;
case "delete":
System.out.printf("请输入需要删除的雇员的 id:");
int DelId = scanner.nextInt();
hashTab.DelEmpById(DelId);
break;
case "exit":
scanner.close();
System.exit(0);
default:
break;
}
}
}
}
//创建 HashTable 管理多条链表
class HashTab {
private EmpLinkedList[] empLinkedListArray;
private int size;//表示共有多少条链表
//构造器
public HashTab(int size) {
this.size = size;
//初始化 empLinkedListArray
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 void list() {
for (int i = 0; i < size; ++i) {
empLinkedListArray[i].list(i);
}
}
//根据输入的id查找雇员
public void findEmpById(int id) {
//使用散列函数确定在哪一个链表里
int empLinkedListNO = hashFun(id);
Emp emp = empLinkedListArray[empLinkedListNO].findEmpById(id);
if (emp != null) {
//找到了
System.out.printf("在第 %d 条链表中找到 雇员 id = %d name=%s\n", empLinkedListNO + 1, emp.id, emp.name);
} else {
//没有找到
System.out.println("没有找到该雇员");
}
}
//根据雇员的 id 删除雇员
public void DelEmpById(int DelId) {
//使用散列函数确定在哪一个链表里
int empLinkedListNO = hashFun(DelId);
boolean judge = empLinkedListArray[empLinkedListNO].DelEmp(DelId);
if (judge) {
System.out.println("~~已删除~~");
} else {
System.out.println("~~没有找到需要删除的 id ~~");
}
}
//编写散列函数,使用一个简单取模法
public int hashFun(int id) {
return id % size;
}
}
//表示一个雇员
class Emp {
public int id;
public String name;
public Emp next;//默认为空
public Emp(int id, String name) {
super();
this.id = id;
this.name = name;
}
}
//创建EmpLinkedList,表示链表
class EmpLinkedList {
//头指针,指向第一个Emp
private Emp head;//默认为空
//添加员工
//直接加入到本链表的最后一个
public void add(Emp emp) {
//如果是添加第一个雇员
if (head == null) {
head = emp;
return;
}
//如果不是第一个雇员,使用辅助指针定位到最后
Emp curEmp = head;
while (true) {
if (curEmp.next == null) {
//说明到链表最后
break;
}
//后移
curEmp = curEmp.next;
}
//退出时直接将emp加入链表
curEmp.next = emp;
}
//遍历链表的雇员信息
public void list(int no) {
if (head == null) {
System.out.println("第" + (no + 1) + " 链表为空");
return;
}
System.out.print("第" + (no + 1) + " 链表的信息为:");
Emp curEmp = head;
while (true) {
System.out.printf("=> id= %d name= %s\t", curEmp.id, curEmp.name);
if (curEmp.next == null) {
System.out.println();
break;
}
curEmp = curEmp.next;
}
}
//根据 id 查找雇员
public Emp findEmpById(int id) {
//判断链表是否为空
if (head == null) {
System.out.println("链表为空");
return null;
}
//辅助指针
Emp curEmp = head;
while (true) {
if (curEmp.id == id) {
//找到了
break;
}
//退出
if (curEmp.next == null) {
//没有找到
curEmp = null;
}
//后移
curEmp = curEmp.next;
}
return curEmp;
}
//删除雇员 输入删除的id
public boolean DelEmp(int DelId) {
//判空
if (head == null) {
System.out.println();
return false;
}
//判断头结点是否是需要删除的结点
if (head.id == DelId) {
head = head.next;
return true;
}
//辅助指针
Emp curEmp = head;
while (true) {
if (curEmp.next.id == DelId) {
//找到了需要删除的结点
curEmp.next = curEmp.next.next;
return true;
}
//退出
if (curEmp.next.next == null) {
//没有找到需要删除的结点
return false;
}
//后移
curEmp = curEmp.next;
}
}
}