哈希表 详细讲解

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 + '\'' +
                '}';
    }
}

猜你喜欢

转载自blog.csdn.net/cativen/article/details/124722230