Java哈希表

哈希表

一、概述

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

二、举例分析

  • 通过哈希表进行员工信息的管理
import java.util.Scanner;

/**
 * @author DELL
 * @Date 2020/2/4 17:57
 **/
public class HashTableTest {
    public static void main(String[] args) {
        HashTable hashTable = new HashTable(10);
        String key = "";
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("add: 添加雇员");
            System.out.println("dis: 显示雇员");
            System.out.println("del: 删除雇员");
            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();
                    Employee emp = new Employee(id, name);
                    hashTable.add(emp);
                    break;
                case "dis":
                    hashTable.display();
                    break;
                case "find":
                    System.out.println("请输入查找的id");
                    int i = scanner.nextInt();
                    hashTable.findEmployee(i);
                    break;
                case "del":
                    System.out.println("请输入员工id:");
                    int n = scanner.nextInt();
                    hashTable.deleteEmployee(n);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                    break;
                default:
                    break;
            }
        }
    }
}

/**
 * @author DELL
 * @Date 2020/2/4 18:27
 **/
public class HashTable {
    EmployeeLinked[] employeeLinkedArray;
    //链表的长度
    private int size;

    public HashTable(int size) {
        this.size = size;
        employeeLinkedArray = new EmployeeLinked[size];
        for (int i = 0; i < size; i++) {
            employeeLinkedArray[i] = new EmployeeLinked();
        }
    }

    /**
     * 添加员工
     *
     * @param emp
     */
    public void add(Employee emp) {
        int employeeLinkedIndex = hashFun(emp.id);
        employeeLinkedArray[employeeLinkedIndex].add(emp);
    }

    /**
     * 散列函数,使用取模法实现
     *
     * @param id
     * @return
     */
    public int hashFun(int id) {
        return id % size;
    }

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

    /**
     * 根据id查找雇员
     *
     * @param id
     */
    public void findEmployee(int id) {
        int i = hashFun(id);
        Employee employee = employeeLinkedArray[i].findEmployee(id);
        if (employee == null) {
            System.out.println("没有找到!");
        } else {
            System.out.println(employee.toString());
        }
    }

    /**
     * 根据id删除雇员
     *
     * @param id
     */
    public void deleteEmployee(int id) {
        int i = hashFun(id);
        boolean delete = employeeLinkedArray[i].delete(id);
        if (delete) {
            System.out.println("删除成功!");
        } else {
            System.out.println("没有此员工!");
        }
    }
}

/**
 * 存储雇员信息的链表
 *
 * @author DELL
 * @Date 2020/2/4 18:30
 **/
public class EmployeeLinked {
    private Employee head;

    /**
     * 添加雇员,尾插
     *
     * @param emp
     */
    public void add(Employee emp) {
        if (this.head == null) {
            head = emp;
            return;
        }
        Employee cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = emp;
    }

    /**
     * 打印所有信息
     *
     * @param no
     */
    public void display(int no) {
        if (this.head == null) {
            System.out.println("第" + no + "条链表为空!");
            return;
        }
        Employee cur = this.head;
        System.out.println("第" + no + "条链表信息为:");
        while (cur != null) {
            System.out.println(cur.toString());
            cur = cur.next;
        }
    }

    /**
     * 根据id查找雇员
     *
     * @param id
     * @return
     */
    public Employee findEmployee(int id) {
        if (this.head == null) {
            return null;
        }
        Employee cur = this.head;
        while (cur.id != id) {
            cur = cur.next;
        }
        return cur;
    }

    /**
     * id的前一个节点
     *
     * @param id
     * @return 找到返回true,否则返回false
     */
    private Employee beforeID(int id) {
        Employee cur = this.head;
        while (cur.next.id != id) {
            cur = cur.next;
        }
        return cur;
    }

    /**
     * 根据id号删除雇员
     *
     * @param id
     * @return 删除成功返回true,否则返回false
     */
    public boolean delete(int id) {
        if (this.head == null) {
            return false;
        }
        if (this.head.id == id) {
            this.head = this.head.next;
            return true;
        }
        Employee beforeID = beforeID(id);
        if (beforeID == null) {
            return false;
        } else {
            beforeID.next = beforeID.next.next;
            return true;
        }
    }
}

/**
 * 雇员
 *
 * @author DELL
 * @Date 2020/2/4 18:28
 **/
public class Employee {
    public int id;
    public String name;
    public Employee next;

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

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
发布了55 篇原创文章 · 获赞 20 · 访问量 3944

猜你喜欢

转载自blog.csdn.net/qq_40613029/article/details/104182139