第 9 章 哈希表
1、Google 上机题
- 看一个实际需求, google 公司的一个上机题:
- 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id, 性别, 年龄, 住址…),当输入该员工的 id 时,要求查找到该员工的所有信息
- 要求:不使用数据库,尽量节省内存,速度越快越好 => 哈希表(散列)
2、哈希表基本介绍
- 散列表(Hash table, 也叫哈希表) ,是根据关键码值(Key value)而直接进行访问的数据结构。
- 它通过把关键码值映射到表中一个位置来访问记录, 以加快查找的速度。 这个映射函数叫做散列函数, 存放记录的数组叫做散列表。
- 哈希表的核心:private EmpLinkedList[] empLinkedListArray;
- 哈希表编程思路:
- 先根据对象的信息将其散列,得到 hashCode
- 根据对象的 hashCode 值,找到对应的数组下标,其实就是找到存储对象的链表
- 在链表中进行相应的增删改查操作
3、代码实现
3.1、Emp 节点
class EmpNode {
public int id;
public String name;
public EmpNode next;
public EmpNode(int id, String name) {
super();
this.id = id;
this.name = name;
}
}
3.2、Emp 链表
- head 是首指针(指向真正存放数据的节点),不是头指针
class EmpLinkedList {
private EmpNode head;
public void add(EmpNode empNode) {
if (head == null) {
head = empNode;
return;
}
EmpNode curEmp = head;
while (true) {
if (curEmp.next == null) {
break;
}
curEmp = curEmp.next;
}
curEmp.next = empNode;
}
public void list(int no) {
if (head == null) {
System.out.println("第 " + (no + 1) + " 链表为空");
return;
}
System.out.print("第 " + (no + 1) + " 链表的信息为");
EmpNode curEmp = head;
while (true) {
System.out.printf(" => id=%d name=%s ", curEmp.id, curEmp.name);
if (curEmp.next == null) {
break;
}
curEmp = curEmp.next;
}
System.out.println();
}
public EmpNode findEmpById(int id) {
if (head == null) {
System.out.println("链表为空");
return null;
}
EmpNode curEmp = head;
while (true) {
if (curEmp.id == id) {
break;
}
curEmp = curEmp.next;
if (curEmp == null) {
break;
}
}
return curEmp;
}
}
3.3、Emp 哈希表
class HashTab {
private EmpLinkedList[] empLinkedListArray;
private int size;
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(EmpNode empNode) {
int empLinkedListNO = hashFun(empNode.id);
empLinkedListArray[empLinkedListNO].add(empNode);
}
public void list() {
for (int i = 0; i < size; i++) {
empLinkedListArray[i].list(i);
}
}
public void findEmpById(int id) {
int empLinkedListNO = hashFun(id);
EmpNode empNode = empLinkedListArray[empLinkedListNO].findEmpById(id);
if (empNode != null) {
System.out.printf("在第%d条链表中找到 雇员 id = %d\n", (empLinkedListNO + 1), id);
} else {
System.out.println("在哈希表中,没有找到该雇员~");
}
}
public int hashFun(int id) {
return id % size;
}
}
3.4、代码测试
public class HashTabDemo {
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("exit: 退出系统");
System.out.println();
key = scanner.next();
switch (key) {
case "add":
System.out.println("输入id");
int id = scanner.nextInt();
System.out.println("输入名字");
String name = scanner.next();
EmpNode empNode = new EmpNode(id, name);
hashTab.add(empNode);
break;
case "list":
hashTab.list();
break;
case "find":
System.out.println("请输入要查找的id");
id = scanner.nextInt();
hashTab.findEmpById(id);
break;
case "exit":
scanner.close();
System.exit(0);
default:
break;
}
}
}
}
add: 添加雇员
list: 显示雇员
find: 查找雇员
exit: 退出系统
add
输入id
1
输入名字
Heygo
add: 添加雇员
list: 显示雇员
find: 查找雇员
exit: 退出系统
add
输入id
2
输入名字
Oneby
add: 添加雇员
list: 显示雇员
find: 查找雇员
exit: 退出系统
add
输入id
8
输入名字
NiuNiu
add: 添加雇员
list: 显示雇员
find: 查找雇员
exit: 退出系统
list
第 1 链表为空
第 2 链表的信息为 => id=1 name=Heygo => id=8 name=NiuNiu
第 3 链表的信息为 => id=2 name=Oneby
第 4 链表为空
第 5 链表为空
第 6 链表为空
第 7 链表为空
add: 添加雇员
list: 显示雇员
find: 查找雇员
exit: 退出系统
find
请输入要查找的id
1
在第2条链表中找到 雇员 id = 1
add: 添加雇员
list: 显示雇员
find: 查找雇员
exit: 退出系统
find
请输入要查找的id
8
在第2条链表中找到 雇员 id = 8
add: 添加雇员
list: 显示雇员
find: 查找雇员
exit: 退出系统
find
请输入要查找的id
9
在哈希表中,没有找到该雇员~
add: 添加雇员
list: 显示雇员
find: 查找雇员
exit: 退出系统
3.5、课后练习
4、Emp 哈希表全部代码
public class HashTabDemo {
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("exit: 退出系统");
System.out.println();
key = scanner.next();
switch (key) {
case "add":
System.out.println("输入id");
int id = scanner.nextInt();
System.out.println("输入名字");
String name = scanner.next();
EmpNode empNode = new EmpNode(id, name);
hashTab.add(empNode);
break;
case "list":
hashTab.list();
break;
case "find":
System.out.println("请输入要查找的id");
id = scanner.nextInt();
hashTab.findEmpById(id);
break;
case "exit":
scanner.close();
System.exit(0);
default:
break;
}
}
}
}
class HashTab {
private EmpLinkedList[] empLinkedListArray;
private int size;
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(EmpNode empNode) {
int empLinkedListNO = hashFun(empNode.id);
empLinkedListArray[empLinkedListNO].add(empNode);
}
public void list() {
for (int i = 0; i < size; i++) {
empLinkedListArray[i].list(i);
}
}
public void findEmpById(int id) {
int empLinkedListNO = hashFun(id);
EmpNode empNode = empLinkedListArray[empLinkedListNO].findEmpById(id);
if (empNode != null) {
System.out.printf("在第%d条链表中找到 雇员 id = %d\n", (empLinkedListNO + 1), id);
} else {
System.out.println("在哈希表中,没有找到该雇员~");
}
}
public int hashFun(int id) {
return id % size;
}
}
class EmpNode {
public int id;
public String name;
public EmpNode next;
public EmpNode(int id, String name) {
super();
this.id = id;
this.name = name;
}
}
class EmpLinkedList {
private EmpNode head;
public void add(EmpNode empNode) {
if (head == null) {
head = empNode;
return;
}
EmpNode curEmp = head;
while (true) {
if (curEmp.next == null) {
break;
}
curEmp = curEmp.next;
}
curEmp.next = empNode;
}
public void list(int no) {
if (head == null) {
System.out.println("第 " + (no + 1) + " 链表为空");
return;
}
System.out.print("第 " + (no + 1) + " 链表的信息为");
EmpNode curEmp = head;
while (true) {
System.out.printf(" => id=%d name=%s ", curEmp.id, curEmp.name);
if (curEmp.next == null) {
break;
}
curEmp = curEmp.next;
}
System.out.println();
}
public EmpNode findEmpById(int id) {
if (head == null) {
System.out.println("链表为空");
return null;
}
EmpNode curEmp = head;
while (true) {
if (curEmp.id == id) {
break;
}
curEmp = curEmp.next;
if (curEmp == null) {
break;
}
}
return curEmp;
}
}