【数据结构】Java实现简单哈希表

使用拉链法实现的简单哈希表,用于加深对于Java的使用和数据结构的理解
代码的put函数有个小bug,为了简单实现直接用的头插法添加元素,但是大部分情况下应该检测是否已经含有了这个元素并更新

Java中的泛型使用擦除法实现的,简单来说就是把类型都转换成Object
这也是为什么泛型不支持基础类型的原因,因为基础类型无法转换成Object

package cn.nya.hash;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Program: first - SimpleHashMap
 * Description: 简单HashMap
 * Author: Nya
 * CreateAt: 2024-03-04 12:01
 **/
public class SimpleHashMap<K, V> {

    private static final int DEFAULT_CAPACITY = 1 << 4;

    private class SimpleHashPair {

        private K key;
        private V value;
        private SimpleHashPair next;
        private int retrievalCount;

        public SimpleHashPair(K key, V value, SimpleHashPair next) {
            this.key = key;
            this.value = value;
            this.next = next;
            this.retrievalCount = 0;
        }

        public SimpleHashPair(K key, V value) {
            this.key = key;
            this.value = value;
            this.next = null;
            this.retrievalCount = 0;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        public int getRetrievalCount() {
            return retrievalCount;
        }

        public void setRetrievalCount(int retrievalCount) {
            this.retrievalCount = retrievalCount;
        }

        public SimpleHashPair getNext() {
            return next;
        }

        public void setNext(SimpleHashPair next) {
            this.next = next;
        }
    }

    // 索引表
    private ArrayList<SimpleHashPair> indexArray;

    public SimpleHashMap() {
        initializeIndexArray(DEFAULT_CAPACITY);
    }

    public SimpleHashMap(int size) {
        initializeIndexArray(size);
    }


    private void initializeIndexArray(int capacity) {
        indexArray = new ArrayList<>(capacity);
        for (int i = 0; i < capacity; i++) {
            indexArray.add(null);
        }
    }

    private int capacity() {
        return indexArray.size();
    }

    private int getHashCode(K key) {
        int hashCode = key.hashCode();
        return hashCode % capacity();
    }

    /**
     * 判断是否含有key
     *
     * @param key 需要查找的key
     * @return 是否包含
     */
    public boolean containsKey(K key) {
        return get(key) != null;
    }

    /**
     * 哈希表加入元素
     *
     * @param key   键
     * @param value 值
     */
    public void put(K key, V value) {
        int index = getHashCode(key);
        SimpleHashPair newPair = new SimpleHashPair(key, value);
        SimpleHashPair head = indexArray.get(index);
        if (head == null) {
            indexArray.set(index, newPair);
            return;
        }
        // 在这里可以遍历链表,寻找是否有需要添加的key,如果有就更新,否则新增
        SimpleHashPair next = head.next;
        head.setNext(newPair.getNext());
        newPair.setNext(next);
    }

    /**
     * 获取哈希表值
     *
     * @param key 键
     * @return 值
     */
    public V get(K key) {
        int index = getHashCode(key);
        SimpleHashPair head = indexArray.get(index);
        if (head == null) {
            return null;
        }

        while (head != null && !head.getKey().equals(key)) {
            head = head.next;
        }

        if (head != null) {
            return head.getValue();
        }
        return null;
    }

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值