哈希表 详细讲解

1、哈希表的基本介绍

散列表(Hash table,也叫哈希表)是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列数存放记录的数组叫做散列表
在这里插入图片描述

2、哈希表(散列-Google)上机题

1)看一个实际需求,google公司的一个上机题:
2)有一个公司,当有新的员工来报道时,要求将该员工的信息加入d,性别,年龄,住址),当输入该员工的id时,要求查找到该员工的所有信息
3)要求:不使用数据库,尽量节省内存速度越快越好=>哈希表(散列
在这里插入图片描述

3、哈希表(散列-Google)上机题思路分析

在这里插入图片描述

4、代码实现

package com.qf.hash;

import java.util.Scanner;

public class HashTabDemo {
    public static void main(String[] args) {
        HashTab hashTab=new HashTab(8);
        boolean loop=true;
        String sc="";
        while (loop){
            System.out.println("add 添加员工");
            System.out.println("list 展示员工");
            System.out.println("find 查找员工");
            System.out.println("exit 退出");
            Scanner systemPut=new Scanner(System.in);
            sc=systemPut.next();
            switch (sc){
                case "add":
                    System.out.println("请输入员工编号:");
                    Scanner addNum=new Scanner(System.in);
                    int id = addNum.nextInt();
                    System.out.println("请输入员工名字:");
                    Scanner nameScan=new Scanner(System.in);
                    String name = nameScan.next();
                    Emp emp=new Emp(id,name);
                    hashTab.add(emp);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("请输入员工编号:");
                    Scanner find=new Scanner(System.in);
                    int findId = find.nextInt();
                    hashTab.find(findId);
                    break;
                case "exit":
                    loop=false;
                    System.out.println("退出循环");
                    break;
                default :
                    break;
            }
        }
    }
}
class HashTab{
    public EmpLinkedList[] linkedListArray;
    public  int maxSize;
    public HashTab(int maxSize){
        this.maxSize=maxSize;
        linkedListArray=new EmpLinkedList[maxSize];
        for (int i = 0; i < linkedListArray.length; i++) {
            linkedListArray[i]=new EmpLinkedList();
        }

    }
    public void add(Emp emp){
        int id=emp.id;
        int location=id%maxSize;
        EmpLinkedList linkedList=linkedListArray[location];
        linkedList.add(emp);
    }

    public void list(){
        for (int i = 0; i < linkedListArray.length; i++) {
            linkedListArray[i].list(i);
        }
    }
    public void find(int id){
        int location=id%maxSize;
        EmpLinkedList linkedList=linkedListArray[location];
        linkedList.find(id);
    }
}
class EmpLinkedList{
    public Emp head;
    public EmpLinkedList(){}

    public void add(Emp emp){
        if (head==null){
            head=emp;
        }else{
            Emp temp=head;
            while (true){
                if (temp.next==null){
                    break;
                }
                temp=temp.next;
            }
            temp.next=emp;
        }
    }

    public void list(int i){
        if (head==null){
            System.out.println("第"+i+"条链表的长度为空");
        }else {
            Emp temp=head;
            while (true){
                System.out.println("第"+i+"条链表员工id为:"+temp.id+"名字为:"+temp.name);
                if (temp.next==null){
                    break;
                }
                temp=temp.next;
            }
        }
    }

    public void find(int id){
        if (head==null){
            System.out.println("链表长度为空,未能找到员工");
        }else{
            Emp temp=head;
            boolean isFind=false;
            while (true){
                if (temp.id==id){
                    isFind=true;
                    System.out.println("员工的名字为:"+temp.name);
                    break;
                }
                if (temp.next==null){
                    break;
                }
                temp=temp.next;
            }
            if (!isFind){
                System.out.println("未能找到员工");
            }
        }
    }
}
class Emp{
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

舒克日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值