介绍
散列表散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。
代码演示
1.员工类
class Emp {
public int id;
public String name;
//next初始为null
public Emp next;
public Emp(int id, String name) {
this.id = id;
this.name = name;
}
2.员工单链表类
class EmpLinkedList {
//初始化头结点
private Emp head=new Emp(-1,"");
//1.添加员工
public void add(Emp emp) {
Emp curEmp = head;
while(true) {
if (curEmp.next == null) {
curEmp.next = emp;
emp.next = null;
System.out.println("添加成功!");
return;
}
curEmp=curEmp.next;
}
}
//2.遍历链表
public void show() {
if (head == null) {
System.out.println("链表为空!");
} else {
Emp temp = head;
while (true) {
System.out.println(temp);
if (temp.next == null) break;
temp = temp.next;
}
}
}
//3.根据id查找员工信息
public Emp findEmpById(int id) {
Emp temp = head;
while (true) {
if (id == temp.id) {
return temp;
}
if (temp.next == null) {
return null;
}
temp = temp.next;
}
}
//4.根据删除员工
public Boolean DelEmpById(int id) {
Boolean flag=false;
Emp temp = head;
while (true) {
if (temp.next==null) break;
if(temp.next.id==id){
flag = true;
break;
}
temp = temp.next;
}
if(flag) {
temp.next=temp.next.next;
return true;
}else return false;
}
//5.根据id修改员工信息
public Boolean updateEmpById(int id,String name) {
Emp temp = head;
while (true) {
if (id == temp.id) {
temp.name=name;
return true;
}
if (temp.next == null) {
return false;
}
temp = temp.next;
}
}
}
3.员工哈希表类
class HashTab {
private int size;
public EmpLinkedList[] linkedListArray;
public HashTab(int size) {
this.size = size;
this.linkedListArray = new EmpLinkedList[size];
for (int i = 0; i < size; i++) {
linkedListArray[i] = new EmpLinkedList();
}
}
//1.添加员工
public void add(Emp emp) {
linkedListArray[hashFunction(emp.id)].add(emp);
}
//2.遍历数组
public void show() {
for (int i = 0; i < size; i++) {
System.out.println("list[" + i + "]:");
linkedListArray[i].show();
}
}
//3.根据id查员工
public void findEmpById(int id) {
if (linkedListArray[hashFunction(id)].findEmpById(id) == null) {
System.out.println("未找到员工信息!");
} else {
System.out.println("已找到!在第" + (hashFunction(id) + 1) + "个表中," + linkedListArray[hashFunction(id)].findEmpById(id));
}
;
}
//3.根据id删除员工
public void DelEmpById(int id) {
if (linkedListArray[hashFunction(id)].DelEmpById(id)) {
System.out.println("删除成功!");
} else System.out.println("未查找到 id 为 " + id + " 的员工,删除失败!");
}
//4.根据id修改员工
public void updateEmpById(int id,String name) {
if (linkedListArray[hashFunction(id)].updateEmpById(id,name)) {
System.out.println("修改成功!");
} else System.out.println("未查找到 id 为 " + id + " 的员工,修改失败!");
}
//5.计算哈希值
private int hashFunction(int id) {
return id % size;
}
}
4.测试
4.1添加与查找
@Test
public void testFind() {
HashTab tab = new HashTab(4);
tab.add(new Emp(1, "wz"));
tab.add(new Emp(2, "zp"));
tab.add(new Emp(3, "ls"));
tab.findEmpById(2);
}
添加成功!
添加成功!
添加成功!
已找到!在第3个表中,Emp{id=2, name='zp'}
4.2 删除
@Test
public void testDel() {
HashTab tab = new HashTab(4);
tab.add(new Emp(1, "wz"));
tab.add(new Emp(2, "zp"));
tab.add(new Emp(3, "ls"));
tab.DelEmpById(2);
tab.findEmpById(2);
}
添加成功!
添加成功!
添加成功!
删除成功!
未找到员工信息!
4.3 更新信息
@Test
public void testUpdate() {
HashTab tab = new HashTab(4);
tab.add(new Emp(1, "wz"));
tab.add(new Emp(2, "zp"));
tab.add(new Emp(3, "ls"));
tab.add(new Emp(4, "zzl"));
tab.add(new Emp(5, "lxh"));
tab.add(new Emp(6, "lx"));
tab.findEmpById(4);
tab.updateEmpById(4,"zz");
tab.findEmpById(4);
}
添加成功!
添加成功!
添加成功!
添加成功!
添加成功!
添加成功!
已找到!在第1个表中,Emp{id=4, name='zzl'}
修改成功!
已找到!在第1个表中,Emp{id=4, name='zz'}
4.4 遍历
@Test
public void testShow() {
HashTab tab = new HashTab(4);
tab.add(new Emp(1, "wz"));
tab.add(new Emp(2, "zp"));
tab.add(new Emp(3, "ls"));
tab.add(new Emp(4, "zzl"));
tab.add(new Emp(5, "lxh"));
tab.add(new Emp(6, "lx"));
tab.add(new Emp(7, "lal"));
tab.add(new Emp(8, "zxw"));
tab.add(new Emp(9, "cf"));
tab.show();
}
list[0]:
Emp{id=-1, name=''}
Emp{id=4, name='zzl'}
Emp{id=8, name='zxw'}
list[1]:
Emp{id=-1, name=''}
Emp{id=1, name='wz'}
Emp{id=5, name='lxh'}
Emp{id=9, name='cf'}
list[2]:
Emp{id=-1, name=''}
Emp{id=2, name='zp'}
Emp{id=6, name='lx'}
list[3]:
Emp{id=-1, name=''}
Emp{id=3, name='ls'}
Emp{id=7, name='lal'}