[Java数据结构][9]以雇员为例的哈希表HashTable代码展开
文章目录
[Java数据结构][9]以雇员为例的哈希表HashTable代码展开定义一个雇员类创建一个含有添加、遍历、查找功能的雇员链表创建一个hashtable,含有添加功能,遍历功能,查找功能以及哈希函数功能(通过所给的雇员id返回一个哈希值,这里使用最简单的取模法)完整代码执行案例
散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。给定表M,存在函数f(key),对任意给定的关键字值key,代入函数后若能得到包含该关键字的记录在表中的地址,则称表M为哈希(Hash)表,函数f(key)为哈希(Hash) 函数。
下图就是一个简易哈希表,采用数组+链表的模式
我们的问题
1.看一个实际需求,google 公司的一个上机题:
2.有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该员工的id 时,要求查找到该员工的所有信息.
3.要求: 不使用数据库,尽量节省内存,速度越快越好=>哈希表(散列)
定义一个雇员类
class Emp{
public int id
;
public String name
;
public Emp next
;
public Emp(int id
,String name
){
super();
this.id
= id
;
this.name
= name
;
}
}
创建一个含有添加、遍历、查找功能的雇员链表
class EmpLinkedList {
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
;
}
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
) {
break;
}
curEmp
= curEmp
.next
;
}
System
.out
.println();
}
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
;
break;
}
curEmp
= curEmp
.next
;
}
return curEmp
;
}
}
创建一个hashtable,含有添加功能,遍历功能,查找功能以及哈希函数功能(通过所给的雇员id返回一个哈希值,这里使用最简单的取模法)
class HashTable {
private EmpLinkedList
[] empLinkedListArray
;
private int size
;
public HashTable(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
) {
int empLinkedListNo
= hashFun(emp
.id
);
empLinkedListArray
[empLinkedListNo
].add(emp
);
}
public void list() {
for (int i
= 0; i
< size
; i
++) {
empLinkedListArray
[i
].list(i
);
}
}
public int hashFun(int id
) {
return id
% size
;
}
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 class HashTabDemo {
public static void main(String
[] args
) {
HashTable hashTable
= new HashTable(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:退出系统");
key
= scanner
.next();
switch (key
) {
case "add":
System
.out
.print("输入id:");
int id
= scanner
.nextInt();
System
.out
.print("输入名字:");
String name
= scanner
.next();
Emp emp
= new Emp(id
, name
);
hashTable
.add(emp
);
break;
case "list":
hashTable
.list();
break;
case "find":
System
.out
.print("输入要查找的id:");
id
= scanner
.nextInt();
hashTable
.findEmpById(id
);
break;
case "exit":
scanner
.close();
System
.exit(0);
default:
break;
}
}
}
}
class HashTable {
private EmpLinkedList
[] empLinkedListArray
;
private int size
;
public HashTable(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
) {
int empLinkedListNo
= hashFun(emp
.id
);
empLinkedListArray
[empLinkedListNo
].add(emp
);
}
public void list() {
for (int i
= 0; i
< size
; i
++) {
empLinkedListArray
[i
].list(i
);
}
}
public int hashFun(int id
) {
return id
% size
;
}
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("在哈希表中,没有找到该雇员");
}
}
}
class Emp {
public int id
;
public String name
;
public Emp next
;
public Emp(int id
, String name
) {
super();
this.id
= id
;
this.name
= name
;
}
}
class EmpLinkedList {
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
;
}
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
) {
break;
}
curEmp
= curEmp
.next
;
}
System
.out
.println();
}
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
;
break;
}
curEmp
= curEmp
.next
;
}
return curEmp
;
}
}
执行案例
add
:添加雇员
list
:显示雇员
find
:查找雇员
exit
:退出系统
add
输入id:
16543135
输入名字:jacky
add
:添加雇员
list
:显示雇员
find
:查找雇员
exit
:退出系统
add
输入id:
16543138
输入名字:tom
add
:添加雇员
list
:显示雇员
find
:查找雇员
exit
:退出系统
add
输入id:
16543420
输入名字:jerry
add
:添加雇员
list
:显示雇员
find
:查找雇员
exit
:退出系统
find
输入要查找的id
:5
在哈希表中,没有找到该雇员
add
:添加雇员
list
:显示雇员
find
:查找雇员
exit
:退出系统
find
输入要查找的id
:6
链表为空
在哈希表中,没有找到该雇员
add
:添加雇员
list
:显示雇员
find
:查找雇员
exit
:退出系统
list
第
1条链表的信息为:
-> id
= 16543135 name
= jacky
第
2条链表为空
~
第
3条链表为空
~
第
4条链表的信息为:
-> id
= 16543138 name
= tom
第
5条链表为空
~
第
6条链表的信息为:
-> id
= 16543420 name
= jerry
第
7条链表为空
~
add
:添加雇员
list
:显示雇员
find
:查找雇员
exit
:退出系统
exit
Process finished with exit code
0