哈希表

1、基本介绍

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

image.png

2、哈希表实例

  • 有一个公司,当新的员工来报道时,要求将该员工的信息加入(id,name),当输入该员工的id时,要求查找到该员工的所有信息
  • 要求
    • 不使用数据路,速度越快越好 => 哈希表(散列表)
    • 添加时,保证按照id从底到高插入
package hashTable;

import java.util.Scanner;

//哈希表
public class HashTableDemo {
    public static void main(String[] args) {
        //创建哈希表
        HashTable hashTable = new HashTable(7);

        String key = "";
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("a:添加雇员");
            System.out.println("l:显示雇员");
            System.out.println("f:查找雇员");
            System.out.println("r:删除雇员");
            System.out.println("e:退出系统");

            key = scanner.next();
            switch (key.charAt(0)){
                case 'a':
                    System.out.println("输入id");
                    int id = scanner.nextInt();
                    System.out.println("输入名字");
                    String name = scanner.next();
                    //创建雇员
                    Emp emp = new Emp(id,name);
                    hashTable.add(emp);
                    break;
                case 'l':
                    hashTable.list();
                    break;
                case 'f':
                    System.out.println("输入查找的id");
                    id = scanner.nextInt();
                    hashTable.findEmpById(id);
                    break;
                case 'r':
                    System.out.println("输入要删除雇员的id");
                    id = scanner.nextInt();
                    hashTable.rmEmpById(id);
                    break;
                case 'e':
                    scanner.close();
                    System.exit(0);
                    break;
                default:
                    break;
            }
        }

    }
}

//创建哈希表,管理多条链表
class HashTable{
    private EmpLinkedList[] empLinkedListArray;
    private int size;

    //构造器
    public HashTable(int size){
        this.size = size;
        //初始化empLinkedListArray
        empLinkedListArray = new EmpLinkedList[size];
        //分别初始化每一条链表
        for (int i=0;i<size;i++){
            empLinkedListArray[i] = new EmpLinkedList();
        }
    }

    //添加雇员
    public void add(Emp emp){
        //根据雇员的id,得到该雇员应该加入到那条链表
        int empLikedListNo = hashFun(emp.id);
        //将雇员添加到对应的链表中
        empLinkedListArray[empLikedListNo].add(emp);
    }

    //根据id查找雇员
    public void findEmpById(int id){
        //使用散列函数确定到那条链表查找
        int empLinkedListNo = hashFun(id);
        Emp emp = empLinkedListArray[empLinkedListNo].findEmById(id);
        if (emp == null){
            System.out.println("没有找到该雇员");
        }else {
            System.out.println("在第"+(empLinkedListNo+1)+"条链表找到该雇员");
        }
    }

    //根据id删除雇员
    public void rmEmpById(int id){
        int empLinkedListNo = hashFun(id);
        empLinkedListArray[empLinkedListNo].rmEmp(id);
    }

    //遍历哈希表(遍历所有的表)
    public void list(){
        for (int i=0;i<size;i++){
            empLinkedListArray[i].list(i);
        }
    }

    //散列函数,使用简单的取模法
    public int hashFun(int id){
        return id % size;
    }
}

//雇员
class Emp{
    public int id;
    public String name;
    public Emp next;
    public Emp(int id,String name){
        this.id = id;
        this.name =name;
    }
}


//创建EmpLinkedList,表示链表
class EmpLinkedList{
    //头指针
    private Emp head;

    //添加雇员到链表,(直接加入到链表最后)
    public void add(Emp emp){
        //如果添加的是第一个雇员
        if (head == null){
            head = emp;
            return;
        }
        //如果不是第一个雇员,则使用辅助指针,定位链表最后,添加
        Emp temp = head;
        while (true){
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }

        //添加
        temp.next = emp;

    }

    //遍历链表的雇员信息
    public void list(int no){
        if (head == null){
            System.out.println("第"+(no+1)+"条链表为空");
            return;
        }
        System.out.print("第"+(no+1)+"条链表信息为=>");
        //使用辅助指针temp 遍历链表
        Emp temp  = head;
        while (true){
            if (temp == null){
                break;
            }
            System.out.print("id:"+temp.id+",name:"+temp.name+" ");
            temp = temp.next;
        }
        System.out.println();
    }

    //根据id查找雇员
    public Emp findEmById(int id){
        //判断链表是否为空
        if (head == null){
            return null;
        }
        Emp temp = head;
        while (true){
            if (temp == null){
                break;
            }

            if (temp.id == id){  //找到
                break;
            }

            temp = temp.next;
        }
        return temp;
    }

    //根据id删除雇员
    public void rmEmp(int id){
        if (head == null){
            System.out.println("该雇员不存在");
            return;
        }

        Emp temp = head;

        if (temp.id == id){
            head=head.next;
            return;
        }

        while (true){
            if (temp.next == null){
                System.out.println("没有找到该雇员");
                break;
            }

            if (temp.next.id == id){  //找到该雇员
                //移除该雇员
                temp.next = temp.next.next;
                break;
            }
            temp = temp.next;
        }
    }
}

posted @ 2022-11-06 22:37  youmo~  阅读(17)  评论(0编辑  收藏  举报