【数据结构】实现一个哈希表,使用哈希桶方式解决哈希冲突

本文介绍了两种哈希表的实现方式。第一种是基础版,使用哈希桶解决冲突,负载因子默认为0.75,并在达到阈值时进行扩容。第二种实现加入了泛型,支持存放任意类型数据,利用对象的hashCode和equals方法处理键值对,确保键的唯一性。同时强调了自定义类型需重写hashCode和equals方法的重要性。
摘要由CSDN通过智能技术生成

一. 实现形式一(键值对只能为整数)

我们可以先实现一个比较简单的哈希表,使用java中解决哈希冲突的方法,即哈希桶(开散列)方式实现,其中注意:

  1. 可以使用内部类方式定义节点
  2. 负载因子默认为0.75
  3. 因为我们使用的是哈希桶方式解决哈希冲突,所以在我们扩容成功之后,原来桶中的数据得重新哈希计算出新的位置,不然就和原来桶中的数据的位置不一样了

相关代码如下

public class HashBucket {

    static class Node {//使用内部类方式定义节点
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }

    private Node[] array;
    public int usedSize;

    public HashBucket() {
        this.array = new Node[10];
        this.usedSize = 0;
    }


    public void put(int key,int val) {//存放数据
        //1、确定下标
        int index = key % this.array.length;
        //2、遍历这个下标的链表
        Node cur = array[index];
        while (cur != null) {
            //更新val
            if(cur.key == key) {
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        //3、cur == null   当前数组下标的链表中没有key
        Node node = new Node(key,val);
        node.next = array[index];
        array[index] = node;
        this.usedSize++;
        //4、判断当前有没有超过负载因子
        if(loadFactor() >= 0.75) {//负载因子我们认为0.75
            //扩容
            resize();
        }
    }

    public int get(int key) {//取出数据
        //以什么方式存储的  那就以什么方式取
        int index = key % this.array.length;

        Node cur = array[index];

        while (cur != null) {
            if(cur.key == key) {
                return cur.val;
            }
            cur = cur.next;
        }

        return -1;
    }


    public double loadFactor() {//计算负载因子
        return this.usedSize*1.0 / this.array.length;
    }

    public void resize() {//扩容函数
        //自己创建新的2倍数组
        Node[] newArray = new Node[2*this.array.length];
        //遍历原来的哈希桶
        //最外层循环 控制数组下标
        for (int i = 0; i < this.array.length; i++) {
            Node cur = array[i];
            Node curNext = null;
            while (cur != null) {
                //记录cur.next
                curNext = cur.next;
                //在新的数组里面的下标
                int index = cur.key % newArray.length;
                //进行头插法
                cur.next = newArray[index];
                newArray[index] = cur;
                cur = curNext;
            }
        }
        this.array = newArray;
    }

二. 实现方式二(改进版)、

上面我们实现的哈希表中的键值对只能存放整型数据,但若是比较复杂的类型,例如字符串,对象等等,此时就需要用到泛型了。其中注意:

  1. 同样可以使用内部类方式定义节点类型
  2. 使用泛型
  3. 将泛型转换成整数时要用到hashCode方法
  4. 利用对象哈希值确定下标,为了防止哈希值太大,应该让其%数组的长度
  5. 遍历数组下标时,利用equals方法比较key是否相同
  6. 存放自定义的数据类型时,一定要重写hashcode和equals方法

相关代码如下

class Person {
    public String id;

    public Person(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id='" + id + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(id, person.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
public class HashBuck2<K,V> {
    static class Node<K,V> {
        public K key;
        public V val;
        public Node<K,V> next;

        public Node(K key,V val) {
            this.key = key;
            this.val = val;
        }
    }


    public Node<K,V>[] array = (Node<K, V>[]) new Node[10];
    public int usedSize;

    public void put(K key,V val) {
        //通过hashcode方法定位数组的下标
        int hash = key.hashCode();
        int index = hash % this.array.length;
        Node<K,V> cur = array[index];
        while (cur != null) {
            //equals 起的作用是遍历当前数组下标的key是否相同
            if(cur.key.equals(key)) {
                cur.val = val;
            }
            cur = cur.next;
        }

        Node<K,V> node = new Node<>(key,val);
        node.next = array[index];
        array[index] = node;
        this.usedSize++;
    }

    public V get(K key) {
        int hash = key.hashCode();
        int index = hash % this.array.length;
        Node<K,V> cur= array[index];
        while (cur != null) {
            if(cur.key.equals(key)) {
                return cur.val;
            }
            cur = cur.next;
        }
        return null;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

春风~十一载

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值