数据结构:哈希表

1. 基本介绍

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

在这里插入图片描述

2. 应用案例

  • 当公司有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,名字,住址…),当输入该员工的id时,要求能查找到该员工的所有信息.
  • 要求:
  1. 不使用数据库,速度越快越好=>哈希表(散列)
  2. 添加时,保证按照id从低到高插入 [如果id不是从低到高插入,但要求各条链表仍是从低到高,怎么解决?]
  3. 使用链表来实现哈希表,该链表不带表头[即:链表的第一个结点就存放员员信息]
  • 示意图

在这里插入图片描述

3. 代码实现

public class HashTabDemo {

    public static void main(String[] args) {
        // 创建哈希表
        HashTab hashTab = new HashTab(7);
        // 添加元素
        People p1 = new People(1, "老大");
        People p2 = new People(2, "老二");
        People p3 = new People(3, "老三");
        People p123 = new People(123, "一二三");
        People p456 = new People(456, "四五六");
        People p789 = new People(789, "七八九");
        hashTab.add(p1);
        hashTab.add(p2);
        hashTab.add(p3);
        hashTab.add(p123);
        hashTab.add(p456);
        hashTab.add(p789);
        // 遍历
        hashTab.list();
        // 查找
        hashTab.findPeopleById(123);
        hashTab.findPeopleById(996);
    }

}

class People {

    public int id;

    public String name;

    // 默认是null
    public People next;

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

// 表示链表
class PeopleLinkedList {
    // 头指针,指向第一个People,默认是null
    private People head;

    // 添加人到链表中
    // 假定添加人时,id是自增长的,因此添加到链表最后一个位置即可
    public void add(People people) {
        // 如果是第一个元素
        if (head == null) {
            head = people;
            return;
        }
        // 如果不是第一个元素,则使用辅助变量,帮助定位到最后
        People curPeople = head;
        while (true) {
            // 说明找到链表最后一个节点
            if (curPeople.next == null) {
                break;
            }
            // 后移一位
            curPeople = curPeople.next;
        }
        // 将添加的元素放到最后一位节点
        curPeople.next = people;
    }

    // 遍历链表
    public void list(int no) {
        if (head == null) {
            System.out.println("第 " + (no + 1) + " 链表为空");
            return;
        }
        System.out.print("第 " + (no + 1) + " 链表数据为");
        People curPeople = head;
        while (true) {
            System.out.printf(" id=%d name=%s\t", curPeople.id, curPeople.name);
            if (curPeople.next == null) {
                break;
            }
            // 后移一位
            curPeople = curPeople.next;
        }
        System.out.println();
    }

    // 根据id找人
    // 如果找到就返回people,没找到返回null
    public People findPeopleById(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        People curPeople = head;
        while (true) {
            // 找到
            if (curPeople.id == id) {
                break;
            }
            // 遍历完,还是没找到
            if (curPeople.next == null) {
                curPeople = null;
                break;
            }
            // 后移一位
            curPeople = curPeople.next;
        }
        return curPeople;
    }
}

// 创建HashTab,管理多条链表
class HashTab {

    private PeopleLinkedList[] peopleLinkedListArray;
    // 数组大小
    private int size;

    // 构造器
    public HashTab(int size) {
        this.size = size;
        // 初始化数组
        this.peopleLinkedListArray = new PeopleLinkedList[size];
        // 初始化链表
        for (int i = 0; i < size; i++) {
            peopleLinkedListArray[i] = new PeopleLinkedList();
        }
    }

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

    // 添加元素
    public void add(People people) {
        // 根据id,找到要加到哪条链表中
        int i = hashFun(people.id);
        // 将people添加到对应链表中
        peopleLinkedListArray[i].add(people);
    }

    // 遍历所有链表
    public void list() {
        for (int i = 0; i < size; i++) {
            peopleLinkedListArray[i].list(i);
        }
    }

    // 根据输入的id,查找元素
    public void findPeopleById(int id) {
        // 确定在哪条链表中
        int i = hashFun(id);
        People peopleById = peopleLinkedListArray[i].findPeopleById(id);
        if (peopleById != null) {
            System.out.printf("在第%d条链表中找到 id = %d 的元素\n", i + 1, id);
        } else {
            System.out.printf("没有找到 id = %d 的元素\n", id);
        }
    }

}

  • 打印结果
第 1 链表为空
第 2 链表数据为 id=1 name=老大	 id=456 name=四五六	
第 3 链表数据为 id=2 name=老二	
第 4 链表数据为 id=3 name=老三	
第 5 链表数据为 id=123 name=一二三	
第 6 链表数据为 id=789 name=七八九	
第 7 链表为空
在第5条链表中找到 id = 123 的元素
没有找到 id = 996 的元素
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值