Java数据结构之哈希表及示例

概述

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

使用哈希表模拟Emp雇员节点CRUD

  • 创建Emp
public 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 + '\'' +
                '}';
    }
}
  • 创建EmpLinkedList 雇员链表
public class EmpLinkedList {

    //链表头
    private Emp head;

    //删除
    public void deleteEmpById(int id) {
        if (head == null) {
            return;
        }
        Emp temp = head;

        while (true) {
            if (temp.id == id) {
                //删除
                head = temp.next;
                break;
            }

            if (temp.next == null) {
                break;
            }

            if (temp.next.id == id) {
                //删除emp节点
                temp.next = temp.next.next;
                break;
            }

            //后移
            temp = temp.next;
        }
    }

    //查找
    public Emp findEmpById(int id) {
        if (head == null) {
            return null;
        }
        Emp temp = head;
        while (true) {
            if (temp.id == id) {
                break;
            }
            if (temp.next == null) {
                temp = null;
                break;
            }
            //后移
            temp = temp.next;
        }
        return temp;
    }

    //遍历
    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.print(temp + " --> ");
            if (temp.next == null) {
                break;
            }
            //后移
            temp = temp.next;
        }
        System.out.println();
    }

    //添加
    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        Emp temp = head;
        while (temp.next != null) {
            //后移
            temp = temp.next;
        }
        temp.next = emp;
    }
}
  • 创建HashTab 哈希表
public class HashTab {

    //链表数组
    private EmpLinkedList[] empLinkedListArray;
    private int size;

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

    //根据id 从哈希表删除emp信息
    public void deleteById(int id) {
        int is = hashFun(id);
        empLinkedListArray[is].deleteEmpById(id);
    }

    //根据id 从哈希表查找emp信息
    public void findEmpById(int id) {
        int is = hashFun(id);
        Emp emp = empLinkedListArray[is].findEmpById(id);
        if (emp != null) {
            System.out.println("id对应的值:" + emp);
        }
    }


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

    //添加emp 到哈希表
    public void add(Emp emp) {

        //根据员工id、选择链表
        int empLinkedListNO = hashFun(emp.id);

        //添加到链表
        empLinkedListArray[empLinkedListNO].add(emp);
    }

    //编写散列函数、使用一个简单取模法
    private int hashFun(int id) {
        return id % size;
    }
}
  • 测试 哈希表
public class HashTabDemo {

    public static void main(String[] args) {

        //id数
        int id = 1;

        //创建哈希表
        HashTab hashTab = new HashTab(7);
        String key = "";
        Scanner input = new Scanner(System.in);

        while (true) {
            System.out.println("add:添加 del:删除  list:显示  find:查找  exit:退出");
            key = input.next();
            switch (key) {
                case "add":
                    System.out.print("输入名字:");
                    String name = input.next();
                    //创建雇员
                    Emp emp = new Emp(id++, name);
                    hashTab.add(emp);
                    break;
                case "del":
                    System.out.println("输入key");
                    hashTab.deleteById(input.nextInt());
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("输入key");
                    hashTab.findEmpById(input.nextInt());
                    break;
                case "exit":
                    input.close();
                    System.exit(0);
                    break;
                default:
                    input.close();
                    System.exit(0);
                    break;
            }
        }
    }

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值