哈希表的应用

哈希表

1.基本介绍

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

2.例题

有一个公司,当有新员工来时,要求将该员工信息加入(id,姓名),当输入id时查询信息

package com.sratct.hashtable;

public class HashTableDemo {
    public static void main(String[] args) {
        Hashtable hashtable = new Hashtable(7);
        Emp emp1 = new Emp(1, "张三");
        Emp emp2 = new Emp(2, "李四");
        Emp emp3 = new Emp(8, "王五");
        hashtable.add(emp1);
        hashtable.add(emp2);
        hashtable.add(emp3);
        hashtable.getList();
        hashtable.delete(1);
        hashtable.getList();
    }
}
    // 创建哈希表
    class Hashtable {
        private EmpLinkedList[] empLinkedListArray;
        private int size;
        public Hashtable(int size) {
            this.size = size;
            empLinkedListArray = new EmpLinkedList[size];  // 初始化哈希表
            for (int i=0; i<size; i++) {
                empLinkedListArray[i] = new EmpLinkedList();  //初始化哈希表中每一个链表
            }
        }
        // 新增雇员
        public void add(Emp emp) {
            int i = hashFun(emp.id);
            empLinkedListArray[i].add(emp);
        }

        // 遍历雇员
        public void getList() {
            for (int i=0; i<size; i++) {
                empLinkedListArray[i].getList();
            }
        }
        // 根据id查找雇员
        public void get(int id) {
            int i = hashFun(id);
            Emp emp = empLinkedListArray[i].get(id);
            if (emp == null) {
                System.out.println("未找到该雇员");
            } else {
                System.out.println(emp.id+ ":" + emp.name);
            }
        }
        //删除雇员
        public void delete(int id) {
            int i = hashFun(id);
            empLinkedListArray[i].delete(id);
        }
        //编写散列函数,取模
        private int hashFun(int id) {
            return id % size;
        }

    }


    // 创建雇员表
    class Emp {
        public int id;
        public String name;
        public Emp next;
        public Emp(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }

    //创建一个链表,存放雇员
    class EmpLinkedList {
        // 创建一个无头指针的链表,第一个节点就为第一个雇员,默认为null
        private Emp head;

         //添加元素
        public void add(Emp emp) {
            // 如果链表为空,直接插入
            if (head == null) {
                head = emp;
                return;
            }
            Emp temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = emp;
        }

        // 遍历雇员
        public void getList() {
            // 判断链表是否为空
            if (head == null) {
                System.out.println("没有雇员");
                return;
            }
            Emp temp = head;
            while (temp != null) {
                System.out.println(temp.id + ":" + temp.name);
                temp = temp.next;
            }
        }

        //根据id查找雇员
        public Emp get(int id) {
            if (head == null) {
                System.out.println("没有雇员");
                return null;
            }
            Emp temp = head;
            while (temp != null) {
                if (temp.id == id) {
                    return temp;  // 找到后返回
                }
                temp = temp.next;
            }
            return null;
        }
        // 删除雇员
        public void delete(int id) {
            if (head == null) {
                System.out.println("没有雇员");
                return;
            }
            Emp temp = head;
            while (true) {
                if (temp.id == id) {
                    head = temp.next;
                    return;
                }
                if (temp.next.id == id) {
                    temp.next = temp.next.next;
                    return;
                }
                 if (temp.next == null) {
                     System.out.println("未找到");
                     return;
                 }
                temp = temp.next;
            }
        }
    }

原文地址:https://www.cnblogs.com/cqyp/p/14735860.html