一. 基本介绍
哈希表是根据关键码值而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查询的速度。这个映射函数叫做散列函数,存放记录用的数组叫做散列表。
二. 哈希表底层
三. 代码
package HashTab;
import java.util.Scanner;
public class HashTabDemo {
public static void main(String[] args) {
HashTab hashTab = new HashTab(7);
Scanner sc = new Scanner(System.in);
String key = "";
while(true){
System.out.println("add :表示添加雇员");
System.out.println("del :表示删除雇员");
System.out.println("find:表示查找雇员");
System.out.println("list:表示展示哈希表");
System.out.println("exit:表示退出程序");
key = sc.next();
switch(key){
case "add":
System.out.print("输入 id:");
int id = sc.nextInt();
System.out.print("输入 name:");
String name = sc.next();
EmpNode empNode = new EmpNode(id, name);
hashTab.add(empNode);
break;
case "del":
System.out.printf("输入要删除的雇员的id:");
id = sc.nextInt();
hashTab.delById(id);
break;
case "find":
System.out.print("输入要查找的雇员的id:");
id = sc.nextInt();
hashTab.findByIs(id);
break;
case "list":
hashTab.list();
break;
case "exit":
System.exit(0);
default:
break;
}
}
}
}
// 哈希表类
class HashTab{
EmpLinkedList[] empLinkedListArr = null;
private int size;
public HashTab(int size){
empLinkedListArr = new EmpLinkedList[size];
this.size = size;
for(int i = 0; i < size; i++){
empLinkedListArr[i] = new EmpLinkedList();
}
}
// 添加雇员
public void add(EmpNode empNode){
// 根据雇员的 id 利用散列函数给这个雇员的信息找到合适的链表
int no = hashFun(empNode.id);
empLinkedListArr[no].add(empNode);
}
// 删除
public void delById(int id){
int no = hashFun(id);
empLinkedListArr[no].del(id);
}
// 根据 id 查找某个雇员
public void findByIs(int id){
int no = hashFun(id);
empLinkedListArr[no].find(id);
}
// 展示
public void list(){
for(int i = 0; i < size; i++){
empLinkedListArr[i].list(i);
}
}
// 散列函数
public int hashFun(int id){
return id % size;
}
}
// 链表类
class EmpLinkedList{
public EmpNode head;
// 在一个链表里边,添加雇员的的方法
public void add(EmpNode empNode){
if(head == null){
head = empNode;
return;
}
EmpNode cur = head;
while(true){
if(cur.next == null){
break;
}
cur = cur.next;
}
cur.next = empNode;
}
// 根据给的 id 删除某个雇员
public void del(int id){
if(head == null){
System.out.println("链表为空,没法删除~");
return;
}
EmpNode cur = head;
EmpNode beAttcked = cur.next;
if(cur.id == id){
cur.next = beAttcked.next;
System.out.println("已删除");
return;
}
while(true){
if(beAttcked.next == null){
break;
}
if(beAttcked.id == id){
cur.next = beAttcked.next;
System.out.println("已删除");
return;
}
cur = cur.next;
beAttcked = beAttcked.next;
}
System.out.println("没有这个人,没法删除~");
}
// 查找给定 id 的雇员信息
public void find(int id){
if(head == null){
System.out.println("链表为空,没有你要找的人~");
return;
}
EmpNode cur = head;
while(true){
if(cur.id == id){
System.out.println("id 为" +id+" 的雇员的名字为 "+cur.name);
break;
}
if(cur.next == null){
System.out.println("没有找到你要找的人~");
break;
}
cur = cur.next;
}
}
// 展示链表中雇员的信息
public void list(int no){
if(head == null){
System.out.println("第"+(no+1)+"条链表为空");
return;
}
System.out.printf("第 %d 条链表中有雇员:",(no + 1));
EmpNode cur = head;
while(true){
System.out.printf("=> id=%d,name=%s ",cur.id,cur.name);
if(cur.next == null){
break;
}
cur = cur.next;
}
System.out.println();
}
}
// 雇员节点
class EmpNode{
public int id;
public String name;
public EmpNode next;
public EmpNode(int id, String name) {
this.id = id;
this.name = name;
}
}