(七)哈希表

1、基本介绍

2、应用实例

package hash;

import java.util.Scanner;

/*
       使用哈希表来管理雇员信息

 */
public class HashTable {
    public static final int maxSize = 7;
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(maxSize);
        String key = "";
        int id = 0;
        String name = "";
        Employee employee;
        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.println("\n--------------------");
            System.out.println("add:添加雇员");
            System.out.println("remove:删除雇员");
            System.out.println("update:修改雇员");
            System.out.println("find:查找雇员");
            System.out.println("list:显示雇员");
            System.out.println("exit:退出系统");
            System.out.print("请选择您要进行的操作:");
            key = scanner.next();
            switch (key) {
                case "add":
                    System.out.print("请输入雇员的id:");
                    id = scanner.nextInt();
                    System.out.print("请输入雇员的姓名:");
                    name = scanner.next();
                    employee = new Employee(id, name);
                    hashTab.add(employee);
                    break;
                case "remove":
                    System.out.print("请输入雇员的id:");
                    id = scanner.nextInt();
                    hashTab.remove(id);
                    break;
                case "update":
                    System.out.print("请输入雇员的id:");
                    id = scanner.nextInt();
                    System.out.print("请输入雇员的姓名:");
                    name = scanner.next();
                    employee = new Employee(id, name);
                    hashTab.update(employee);
                    break;
                case "find":
                    System.out.print("请输入雇员的id:");
                    id = scanner.nextInt();
                    hashTab.find(id);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "exit":
                    scanner.close();
                    break;
                default:
                    new RuntimeException("系统异常,请稍后重试!");
                    break;
            }
        }
    }
}

//  哈希表(雇员链表数组)
class HashTab {
    private EmployeeLinkedList[] employeeLinkedLists;
    private int maxSize;
    public HashTab(int maxSize) {
        this.maxSize = maxSize;
        employeeLinkedLists = new EmployeeLinkedList[maxSize];
        // 一定还要再对每个雇员链表节点进行初始化,不然会报空指针异常
        for (int i = 0; i < employeeLinkedLists.length; i++) {
            employeeLinkedLists[i] = new EmployeeLinkedList();
        }
    }

    //  添加雇员
    public void add(Employee employee) {
        //  进行哈希运算,判断加到哪一条雇员链表上
        int employeeLinkedListId = hash(employee.id);
        employeeLinkedLists[employeeLinkedListId].add(employee);
    }

    //  删除雇员
    public void remove(int id) {
        //  进行哈希运算,判断加到哪一条雇员链表上
        int employeeLinkedListId = hash(id);
        boolean flag = employeeLinkedLists[employeeLinkedListId].remove(id);
        if (flag) {
            System.out.println("删除成功!");
        } else {
            System.out.println("删除失败!");
        }
    }

    //  修改雇员
    public void update(Employee employee) {
        //  进行哈希运算,判断加到哪一条雇员链表上
        int employeeLinkedListId = hash(employee.id);
        boolean flag = employeeLinkedLists[employeeLinkedListId].update(employee);
        if (flag) {
            System.out.println("修改成功!");
        } else {
            System.out.println("修改失败!");
            System.out.println("该雇员不存在!");
        }
    }

    //  查找雇员
    public void find(int id) {
        //  进行哈希运算,判断加到哪一条雇员链表上
        int employeeLinkedListId = hash(id);
        boolean flag = employeeLinkedLists[employeeLinkedListId].find(id);
        if (flag) {
            System.out.printf("在第%d条链中查找到id=%d的雇员信息", (employeeLinkedListId + 1), id);
        } else {
            System.out.println("未查找到该雇员的相关信息!");
        }
    }

    //  显示雇员
    public void list() {
        for (int i = 0; i < employeeLinkedLists.length; i++) {
            employeeLinkedLists[i].list(i);
        }
    }

    //  散列函数
    public int hash(int id) {
        return id % this.maxSize;
    }
}


//  雇员链表
class EmployeeLinkedList {
    private Employee head;
    public EmployeeLinkedList(){
        this.head = null;
    }

    //  添加雇员
    public void add(Employee employee) {
        if (head == null) {
            head = employee;
            return;
        }
        Employee temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = employee;
    }

    //  删除雇员
    public boolean remove(int id) {
        if (head == null) {
            return false;
        }
        if (head.next == null) { // 处理只有一个雇员节点
            head = null;
            return true;
        }
        Employee pre = null;
        Employee temp = head;
        while(temp != null) {
            if (temp.id == id) {
                break;
            }
            pre = temp;
            temp = temp.next;
        }
        if (temp != null && pre == null){
            head = temp.next;
            return true;
        } else if (temp != null) {
            pre.next = temp.next;
            return true;
        }
        return false;
    }

    //  修改雇员
    public boolean update(Employee employee) {
        if (head == null) {
            return false;
        }
        Employee temp = head;
        while(temp != null) {
            if (temp.id == employee.id) {
                temp.name = employee.name;
                return true;
            }
            temp = temp.next;
        }
        return false;
    }

    //  查找雇员
    public boolean find(int id) {
        if (head == null) {
            return false;
        }
        Employee temp = head;
        while(temp != null) {
            if (temp.id == id) {
                return true;
            }
            temp = temp.next;
        }
        return false;
    }

    public void list(int i) {
        if (head == null) {
            System.out.println("第" + (i + 1) + "条雇员链表的信息为空...");
            return;
        }
        System.out.print("第"+(i+1)+"条雇员链表的信息");
        Employee temp = head;
        while (temp != null) {
            System.out.print(" -> id=" + temp.id + ",name=" + temp.name);
            temp = temp.next;
        }
        System.out.println();
    }
}


//  雇员节点
class Employee {
    public int id;
    public String name;
    public Employee next;
    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
        this.next = null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来得晚一些也行

观众老爷,请赏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值