哈希表

哈希表

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


哈希表内存模型


思路分析

例有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,名字,住址..),当输入该员工的 id 时, 要求查找到该员工的 所有信息.


代码实现

import java.util.Scanner;
import java.util.concurrent.ForkJoinWorkerThread;

public class 哈希表 {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(7);
        //写一个简单的菜单
        String key = "";
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("add: 添加雇员");
            System.out.println("list: 显示雇员");
            System.out.println("find: 查找雇员");
            System.out.println("exit: 退出系统");

            key = scanner.next();

            switch (key) {
                case "add":
                    System.out.println("输入 id");
                    int id = scanner.nextInt();
                    System.out.println("输入名字");
                    String name = scanner.next();
                    //创建 雇员
                    Emp emp = new Emp(id, name);
                    hashTab.add(emp);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("请输入要查找的 id");
                    id = scanner.nextInt();
                    hashTab.find(id);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                default:
                    break;
            }
        }
    }
}

//创建员工节点
class Emp {
    public int id;
    public String name;
    public Emp next;

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

//创建链表
class EmpList {
    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) + " 链表的信息为");
        Emp temp = head;
        while (true) {
            System.out.printf(" => id=%d name=%s\t", temp.id, temp.name);
            if (temp.next == null) {//说明找到链表最后
                break;
            }
            temp = temp.next;
        }
        System.out.println();
    }

    //根据 id 查找雇员
    public Emp foundEmp(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        Emp temp = head;

        while (true) {
            if (temp.id == id) {
                break;
            }
            if (temp.next == null) {//说明遍历当前链表没有找到该雇员
                temp = null;
                break;
            }
            temp = temp.next;
        }
        return temp;
    }
}

//创建哈希表
class HashTab {
    private EmpList[] empLists;//链表数组,数组里面存放的是链表
    private int size;//表示有多少条链表

    //构造器
    public HashTab(int size) {
        this.size = size;
        //初始化 empLists
        empLists = new EmpList[size];
        //这时要分别初始化每个链表
        for (int i = 0; i < size; i++) {
            empLists[i] = new EmpList();
        }
    }

    //添加员工
    public void add(Emp emp) {
        //根据员工的 id ,得到该员工应当添加到哪条链表
        int no = No(emp.id);
        //将 emp 添加到对应的链表中
        empLists[no].add(emp);
    }

    //遍历所有的链表,遍历 hashtab
    public void list() {
        for (int i = 0; i < size; i++) {
            empLists[i].list(i);
        }
    }

    //根据id查找员工信息
    public void find(int id) {
        //确定是那条链表
        int no = No(id);
        Emp emp = empLists[no].foundEmp(id);
        if (emp != null) {//找到
            System.out.printf("在第%d 条链表中找到 雇员 id = %d\n", (no + 1), id);
        } else {
            System.out.println("在哈希表中,没有找到该雇员~");
        }
    }


    //散列函数
    public int No(int id) {
        //根据员工的id ,得到该员工应当添加到哪条链表
        return id % size;
    }

}

posted @ 2021-02-03 01:06  胡木杨  阅读(54)  评论(0)    收藏  举报