哈希表
看一个实际需求,google公司的一个上机题:
有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址…),当输入该员工的id时,要求查找到该员工的 所有信息.
要求: 不使用数据库,尽量节省内存,速度越快越好=>哈希表(散列)
散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。
自己创建HashTable,需要创建类
- HashTab.java :用来实现对HeadLink,初始化,确定映射函数
HashTab:
int size;
HeadLink headlinks;
构造器(size){
headlinks=new HeadLink(size);
对每个headlinks[i]初始化创建对象;
}
//确定在哪条链表 - 映射函数
index(Int id){
index=id%size;
}
add(){...}
delete(){...}
query(){...}
list(){...}
![](http://music-yyq.oss-cn-beijing.aliyuncs.com/music/2021/10/25/5cb5ef5a-3bf8-493a-a90d-dca653206f66.png)
- HeadLink:相当于上图的东西
HeadLink:
int sno; ---->0
Emp head;--->指向第一个元素 496
delete()
add()
query()
....
- Emp:dao
Emp:
id
name
sala
链表数组
![](http://music-yyq.oss-cn-beijing.aliyuncs.com/music/2021/10/25/32ab35b8-442e-48d9-b038-087634ed2d44.png)
主文件
package HashTab;
import java.awt.*;
import java.util.TreeMap;
/**
* @author yyq
* @create 2021-10-25 11:08
* 哈希表
* 结构为
* 指向员工 HeadLink{
* employee head;
* int sno;
* find();
* delete();
* add();
* list();
* change();
* }
*
* 员工节点 employee{
*...
* }
*
* hashTable{
* int size;
* HeadEmployee[] arr;
* find,
* add
* ...
* }
*
*
*/
public class Hashtab {
private Integer size;
HeadLink[] headLinks;
public Hashtab(Integer size) {
this.size=size;
headLinks=new HeadLink[size];
for (Integer i = 0; i < size; i++) {
headLinks[i]=new HeadLink(i);
}
}
public void add(Employee employee){
int index=employee.id%size;
headLinks[index].add(employee);
}
public void list(){
for (Integer i = 0; i < size; i++) {
headLinks[i].list();
}
}
public Employee query(Integer id){
Integer index=id%size;
Employee query = headLinks[index].query(id);
return query;
}
public void delete(Integer id){
Integer index=id%size;
headLinks[index].delete(id);
}
}
class Employee{
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
public Employee next; //指向下一个节点
public Integer id;
public String name;
public Integer salary;
public Employee(Integer id, String name, Integer salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
}
class HeadLink{
public Employee head=null;
public Integer sno;
public HeadLink(Integer sno) {
this.sno = sno;
}
//添加
//添加后此链表的员工ID有序
public void add(Employee employee){
if(head==null){
head=employee;
return;
}
if(head!=null){
//如果小于第一个元素ID直接添加
if(head.id>employee.id){
employee.next=head;
head=employee;
}
//大于第一个元素ID直接从第二个元素ID比较
else {
Employee pre =head;
Employee last=head.next;
while (true){
if(last==null){
pre.next=employee;
break;
}
//大于last元素ID 两个指针后移 继续循环
if(employee.id>last.id){
pre=last;
last=last.next;
continue;
}
//小于last元素ID直接插入
if(employee.id<=last.id){
employee.next=pre.next;
pre.next=employee;
break;
}
}
}
}
}
//全部信息
public void list(){
if(head==null){
System.out.println("第"+sno+"号链表为空!");
return;
}
Employee temp=head;
System.out.print("第"+sno+"号链表");
while (true){
//退出循环条件 当指向空指针时
if(temp==null) break;
System.out.print("=> 员工ID"+temp.id+", ");
System.out.print("=> 员工姓名"+temp.name+", ");
System.out.print("=> 员工薪资"+temp.salary);
System.out.println();
temp=temp.next;
}
}
//删除
public void delete(Integer id){
Employee pre=head;
Employee last=head.next;
//如果正好是第一个 删除掉
if(pre.id==id){
head=pre.next;
return;
}
//不是第一个的话
while(true){
if(pre.next==null){
System.out.println("没有查看该员工信息");
break;
}
if(last.id==id){
pre.next=last.next;
break;
}
}
}
//查询
public Employee query(Integer id){
Employee temp=head;
while(true){
if (temp==null){
System.out.println("没有找到");
return null;
}
if(temp.id==id) return temp;
temp=temp.next;
}
}
}
测试类
package HashTab;
import java.util.HashMap;
/**
* @author yyq
* @create 2021-10-25 14:55
*/
public class HashTabTest {
public static void main(String[] args) {
Hashtab hashtab = new Hashtab(10);
hashtab.add(new Employee(0,"yyq",1131));
hashtab.add(new Employee(1,"yyq",1131));
hashtab.add(new Employee(10,"yyq",1131));
hashtab.add(new Employee(20,"y1yq",1131));
hashtab.list();
Employee employee = hashtab.query(1);
if (employee==null) System.out.println("没找到");
else System.out.println(employee);
hashtab.delete(1);
System.out.println("********");
hashtab.list();
}
}