栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据结构 - 哈希表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构 - 哈希表

基本介绍

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

数组 + 链表

实例

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

要求:不使用数据库,尽量节省内存,速度越快越好 => 哈希表(散列表)。

员工实体类Employee

class Employee {

    private int id;

    private String name;

    private Employee next; // 下个节点

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Employee getNext() {
        return next;
    }

    public void setNext(Employee next) {
        this.next = next;
    }

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

员工链表EmployeeLinkedList

class EmployeeLinkedList {

    // 头指针,指向第一个Emp,因此我们这个链表的head是直接指向第一个Emp
    // 默认为空
    private Employee head;


    
    public void add(Employee employee) {
        // 如果是第一个雇员
        if (head == null) {
            head = employee;
            return;
        }

        // 如果不是第一个雇员,则使用一个辅助指针,帮助定位到最后
        Employee curEmployee = head;
        while (curEmployee.getNext() != null) {
            curEmployee = curEmployee.getNext();
        }
        // employee加入链表
        curEmployee.setNext(employee);
    }

    
    public void list(int no) {
        if (head == null) {
            System.out.println("第" + no + "条链表为空");
        } else {
            System.out.print("第" + no + "条链表信息为:");
            Employee curEmployee = head;
            while (curEmployee != null) {
                System.out.printf("=> id = %d, name = %st", curEmployee.getId(), curEmployee.getName());
                curEmployee = curEmployee.getNext();
            }
            System.out.println();
        }
    }

    
    public void delete(int id) {
        // 判断链表是否为空
        if (head == null) {
            System.out.println("当前链表为空");
            return;
        }

        if (head.getId() == id) {
            head = head.getNext();
            return;
        }

        Employee curEmployee = head;
        boolean flag = false;// 标志是否找到待删除节点
        while(true) {
            // 已经到链表的最后一个节点
            if(curEmployee.getNext() == null){
                break;
            }
            // 已经找到
            if(curEmployee.getNext().getId() == id) {
                flag = true;
                break;
            }
            // 移动继续找
            curEmployee = curEmployee.getNext();
        }

        // 通过 flag 进行判断
        if(flag) {
            // 已经找到,指向后一个引用
            curEmployee.setNext(curEmployee.getNext().getNext());
        }else {
            System.out.printf("删除的数据不存在【%d】",id);
        }

    }

    
    public Employee find(int id) {
        // 判断链表是否为空
        if (head == null) {
            System.out.println("当前链表为空");
            return null;
        }

        Employee curEmployee = head;
        while (curEmployee != null) {
            if (curEmployee.getId() == id) {
                return curEmployee;
            }
            curEmployee = curEmployee.getNext();
        }

        System.out.println("该元素不存在");
        return null;
    }
}

哈希表HashTable

class HashTable {

    private final EmployeeLinkedList[] employeeLinkedListArray;

    private final int size; // 多少条链表

    
    public HashTable(int size) {
        this.size = size;
        // 初始化employeeLinkedListArray
        employeeLinkedListArray = new EmployeeLinkedList[size];
        // 分别初始化每个链表
        for (int i = 0; i < size; i++) {
            employeeLinkedListArray[i] = new EmployeeLinkedList();
        }
    }

    
    public void add(Employee employee) {
        // 根据员工的id,得到该员工应当添加到哪条链表
        int employeeLinkedListNo = hashFun(employee.getId());
        // 将employee添加到对应的链表
        employeeLinkedListArray[employeeLinkedListNo].add(employee);
    }

    
    public void delete(int id) {
        // 根据员工的id,得到该员工应在到哪条链表
        int employeeLinkedListNo = hashFun(id);
        // 删除对应节点的链表
        employeeLinkedListArray[employeeLinkedListNo].delete(id);
    }

    
    public Employee find(int id) {
        int employeeLinkedListNo = hashFun(id);
        Employee employee = employeeLinkedListArray[employeeLinkedListNo].find(id);
        return employee;
    }

    
    public void list() {
        for (int i = 0; i < size; i++) {
            employeeLinkedListArray[i].list(i);
        }
    }

    
    public int hashFun(int id) {
        return id % size;
    }
}

哈希表测试

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

        // 写一个简单的菜单
        String key;
        Scanner scanner = new Scanner(System.in);
        System.out.println("add:   【添加员工】");
        System.out.println("list:  【显示员工】");
        System.out.println("find:  【查找员工】");
        System.out.println("delete:【删除员工】");
        System.out.println("exit:  【退出系统】");
        int id;
        Employee employee;
        while (true) {
            key = scanner.next();
            switch (key) {
                case "add":
                    System.out.println("输入id:");
                    id = scanner.nextInt();
                    System.out.println("输入名字:");
                    String name = scanner.next();
                    // 创建雇员
                    employee = new Employee(id, name);
                    hashTable.add(employee);
                    System.out.println("添加成功,请重新输入选择菜单:");
                    break;
                case "list":
                    hashTable.list();
                    System.out.println("请重新输入选择菜单:");
                    break;
                case "find":
                    System.out.println("请输入要查找的id:");
                    id = scanner.nextInt();
                    employee = hashTable.find(id);
                    if (employee != null) {
                        System.out.println("查找成功,信息为:" + employee);
                    }
                    System.out.println("请重新输入选择菜单:");
                    break;
                case "delete":
                    System.out.println("请输入要删除的id:");
                    id = scanner.nextInt();
                    hashTable.delete(id);
                    System.out.println("删除成功,请重新输入选择菜单:");
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                default:
                    System.out.println("你的输入有误!请重写输入:");
                    break;
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/886072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号