1、哈希表的基本介绍
哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中的一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。
需求:有一个公司,当有新员工来报道时,要求将该员工的信息加入,当输入该员工的id时,要求查找到该员工的信息。当该员工离开公司时,删除他的信息。
要求:不使用数据库,速度越快越好
添加时,id从低到高。
使用链表来实现哈希表,该链表不带头节点
代码实现:
Emp类:
public class Emp {
int id;
String name;
Emp next;
public Emp() {
}
public Emp(int id, String name) {
this.id = id;
= name;
}
}
EmpLinkedList类:
//创建EmpLinkedList类,表示链表
public class EmpLinkedList {
//头指针,head直接指向第一个Emp
Emp head; //默认为null
//往链表中添加雇员
public void add(Emp emp) {
if (head == null) { //如果是添加第一位雇员
head = emp;
return;
}
Emp temp = head; //创辅助指针
while (true) {
if (temp.next == null) { //到链表最后
break;
}
temp = temp.next;
}
temp.next = emp; //将雇员添加到链表最后
}
//遍历链表(通过编号显示雇员信息)
public void show(int no) {
if (head == null) {
System.out.println("第 " + (no + 1) + " 链表为空");
return;
}
System.out.print("第 " + (no + 1) + " 链表的信息为:");
Emp temp = head;
while (true) {
System.out.print("id= " + + " name= " + + " ");
if (temp.next == null) {
break;
}
temp = temp.next;
}
System.out.println();
}
//根据id查找雇员
//如果找到,就返回Emp,如果没有找到,就返回null
public Emp searchEmpById(int id) {
//判断链表是否为空
if (head == null) {
System.out.println("链表为空");
return null;
}
//辅助指针
Emp temp = head;
while (true) {
if ( == id) { //找到了
break;
}
if (temp.next == null) { //说明在该链表没有找到该雇员
temp = null;
break;
}
temp = temp.next;
}
return temp;
}
//根据id来删除雇员信息
public void deleteEmpById(int id) {
Emp temp = new Emp();
//让temp指向head的前一个节点
temp.next = head;
//删除头节点
if( == id){
head = head.next;
}
boolean flag = false;
while (true) {
if (temp == null) { //说明已经到了链表最后
break;
}
if ( == id) { //找到了,退出循环
flag = true;
break;
}
temp = temp.next;
}
if(flag) {
temp.next = temp.next.next; //找到了,flag的值为true,进行删除操作。
System.out.println("已成功删除节点为" + id + "的雇员信息");
} else{
System.out.println("要删除的id为" + id +"的节点不存在");
}
}
}
HashTab类:
public class HashTab {
EmpLinkedList[] empLinkedListArr;
int size;
public HashTab(int size) {
this.size = size;
//初始化empLinkedListArr
empLinkedListArr = new EmpLinkedList[size];
//初始化每个链表
for (int i = 0; i < size; i++) {
empLinkedListArr[i] = new EmpLinkedList();
}
}
public void add(Emp emp) {
//根据员工的id,得到该员工应当添加到哪条链表
int empLinkedListNo = hashFun(emp.id);
//将emp添加到对应的链表中
empLinkedListArr[empLinkedListNo].add(emp);
}
public void show() {
for (int i = 0; i < size; i++) {
empLinkedListArr[i].show(i);
}
}
public void searchEmpById(int id) {
//使用散列函数确定到哪条链表去查找
int empLinkedListNo = hashFun(id);
Emp emp = empLinkedListArr[empLinkedListNo].searchEmpById(id);
if(emp != null) {//说明找到
System.out.println("在第" + (empLinkedListNo+1) + "条链表中找到该雇员。id为 "+ id);
} else {
System.out.println("在哈希表中没有找到该雇员");
}
}
public void deleteEmpById(int id) {
//使用散列函数确定去哪条链表去删除
int empLinkedListId = hashFun(id);
//进行删除
empLinkedListArr[empLinkedListId].deleteEmpById(id);
}
//编写一个散列函数(哈希函数),使用一个简单的取模法
public int hashFun(int id) {
return id % size;
}
}
测试程序:
import java.util.Scanner;
public class HashTabDemo {
public static void main(String[] args) {
//创建哈希表
HashTab hashTab = new HashTab(7);
String str = "";
Scanner in = new Scanner(System.in);
while(true) {
System.out.println("add: 添加雇员");
System.out.println("show: 显示雇员");
System.out.println("search: 查找雇员");
System.out.println("delete: 删除雇员");
System.out.println("exit: 退出系统");
str = in.next();
switch (str) {
case "add":
System.out.println("输入id");
int id = in.nextInt();
System.out.println("输入名字");
String name = in.next();
Emp emp = new Emp(id,name);
hashTab.add(emp);
break;
case "show":
hashTab.show();
break;
case "search":
System.out.println("请输入要查找的id");
id = in.nextInt();
hashTab.searchEmpById(id);
break;
case "delete":
System.out.println("请输入要删除的id");
id = in.nextInt();
hashTab.deleteEmpById(id);
break;
case "exit":
in.close();
System.exit(0);
default:
break;
}
}
}
}