hashmap 简单实现

import java.util.LinkedList;

public class MyHashMap<K,V> {
    private int capacity;
    private float loadFactor;
    private LinkedList<Node>[] array;
    private int size;

    public MyHashMap(int capacity, float loadFactor) {
        this.capacity = capacity;
        this.loadFactor = loadFactor;
        array = new LinkedList[capacity];
        size = 0;
    }

    public V get(K key) {
        int index = key.hashCode() % capacity;
        LinkedList<Node> list = array[index];
        if (list != null) {
            for (Node node : list) {
                if (node.key.equals(key)) {
                    return node.value;
                }
            }
        }
        return null;
    }

    public void put(K key, V value) {
        int index = key.hashCode() % capacity;
        LinkedList<Node> list = array[index];
        if (list == null) {
            list = new LinkedList<Node>();
            array[index] = list;
        }
        for (Node node : list) {
            if (node.key.equals(key)) {
                node.value = value;
                return;
            }
        }
        list.add(new Node(key, value));
        size++;
        if (size / (float)capacity > loadFactor) {
            resize();
        }
    }

    private void resize() {
        capacity = capacity * 2;
        LinkedList<Node>[] newArray = new LinkedList[capacity];
        for (LinkedList<Node> list : array) {
            if (list != null) {
                for (Node node : list) {
                    int index = node.key.hashCode() % capacity;
                    LinkedList<Node> newList = newArray[index];
                    if (newList == null) {
                        newList = new LinkedList<Node>();
                        newArray[index] = newList;
                    }
                    newList.add(node);
                }
            }
        }
        array = newArray;
    }

    private class Node {
        K key;
        V value;

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HashMap 是一种常用的数据结构,它可以用来存储键值对,通过键来快速查找对应的值。以下是一个简单HashMap 实现: ```java public class HashMap<K, V> { private static final int DEFAULT_CAPACITY = 16; private static final float DEFAULT_LOAD_FACTOR = 0.75f; private Entry<K, V>[] table; private int size; private int threshold; private float loadFactor; public HashMap() { this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); } public HashMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); } public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity <= 0 || loadFactor <= 0 || Float.isNaN(loadFactor)) { throw new IllegalArgumentException(); } this.loadFactor = loadFactor; this.table = new Entry[initialCapacity]; this.threshold = (int) (initialCapacity * loadFactor); } public void put(K key, V value) { if (key == null) { putForNullKey(value); return; } int hash = hash(key.hashCode()); int index = indexFor(hash, table.length); for (Entry<K, V> e = table[index]; e != null; e = e.next) { if (e.hash == hash && (e.key == key || key.equals(e.key))) { e.value = value; return; } } addEntry(hash, key, value, index); } public V get(K key) { if (key == null) { return getForNullKey(); } int hash = hash(key.hashCode()); int index = indexFor(hash, table.length); for (Entry<K, V> e = table[index]; e != null; e = e.next) { if (e.hash == hash && (e.key == key || key.equals(e.key))) { return e.value; } } return null; } private void putForNullKey(V value) { for (Entry<K, V> e = table[0]; e != null; e = e.next) { if (e.key == null) { e.value = value; return; } } addEntry(0, null, value, 0); } private V getForNullKey() { for (Entry<K, V> e = table[0]; e != null; e = e.next) { if (e.key == null) { return e.value; } } return null; } private void addEntry(int hash, K key, V value, int bucketIndex) { Entry<K, V> e = table[bucketIndex]; table[bucketIndex] = new Entry<>(hash, key, value, e); if (size++ >= threshold) { resize(2 * table.length); } } private void resize(int newCapacity) { Entry<K, V>[] oldTable = table; int oldCapacity = oldTable.length; if (oldCapacity == Integer.MAX_VALUE) { threshold = Integer.MAX_VALUE; return; } Entry<K, V>[] newTable = new Entry[newCapacity]; transfer(newTable); table = newTable; threshold = (int) (newCapacity * loadFactor); } private void transfer(Entry<K, V>[] newTable) { Entry<K, V>[] src = table; int newCapacity = newTable.length; for (int j = 0; j < src.length; j++) { Entry<K, V> e = src[j]; if (e != null) { src[j] = null; do { Entry<K, V> next = e.next; int i = indexFor(e.hash, newCapacity); e.next = newTable[i]; newTable[i] = e; e = next; } while (e != null); } } } private int hash(int h) { h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); } private int indexFor(int hash, int length) { return hash & (length - 1); } static class Entry<K, V> { final int hash; final K key; V value; Entry<K, V> next; Entry(int hash, K key, V value, Entry<K, V> next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } } } ``` 这个实现使用了数组和链表来存储键值对,当添加新的键值对时,会根据键的哈希值来计算出它在数组中的位置,如果该位置已经有了其他键值对,就使用链表来解决冲突。当数组中的键值对数量超过了阈值时,会自动扩容数组。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值