模拟实现HashMap

本文介绍了如何模拟实现HashMap,包括关键方法如get、put和扩容。着重强调了正确使用hashCode和equals方法的重要性,并提及Java中HashSet和HashMap的区别,以及它们与LinkedHashSet和LinkedHashMap的区别,后者在元素插入顺序上有所保留。
摘要由CSDN通过智能技术生成

模拟实现HashMap


关于Set和Map区别:

见链接: Set和Map

关于HashTable
见链接: 哈希表

模拟实现HashMap

Node:

public class Node {
    public String key;

    public Integer value;
    public Node next;

    public Node(String key, Integer value) {
        this.key = key;
        this.value = value;
    }
}

模拟实现:

public class MyHashMap {
    private Node[] array;
    private int size;

    public MyHashMap() {
        array = new Node[16];
        size = 0;
    }

    //计算出index
    private int indexOf(String key) {
        int hash = key.hashCode();
        int h = hash ^ (hash >> 16);
        //前提:array.length.一定2的多少次方,16/32/64/128.

        return h & (array.length - 1);
    }

    //get
    public Integer get(String key) {
        int index = indexOf(key);
        Node cur = array[index];
        while (cur != null) {
            if (key.equals(cur.key)) {
                return cur.value;
            }
            cur = cur.next;
        }
        return null;
    }

    //getOrDefault
    public Integer getOrDefault(String key, Integer value) {
        int index = indexOf(key);
        Node cur = array[index];
        while (cur != null) {
            if (key.equals(cur.key)) {
                return cur.value;
            }
            cur = cur.next;
        }
        return value;
    }

    //put
    public Integer put(String key, Integer value) {
        int index = indexOf(key);
        Node cur = array[index];
        while (cur != null) {
            if (key.equals(cur.key)) {
                Integer oldValue = cur.value;
                cur.value = value;
                return oldValue;
            }
            cur = cur.next;
        }

        Node node = new Node(key, value);
        node.next = array[index];
        array[index] = node;
        size++;

        //考虑是否扩容问题
        if ((double) size / array.length > 0.5) {
            grow();
        }
        return null;
    }

    private void grow() {
        Node[] newArray = new Node[array.length * 2];

        for (Node head : array) {
            Node cur = head;
            while (cur != null) {
                int index = indexOf(cur.key) % newArray.length;
                Node next = cur.next;

                cur.next = newArray[index];
                newArray[index] = cur;

                cur = next;
            }
        }
        array = newArray;
    }

}

注意:
没有将所有的方法都实现,但是大致思路都一致;
hashSet和hashMap差不多,(hashSet的底层是hashMap实现)
另外:当key的值不是Int类型的时候,我们需要利用 hashCode(),将其转换为int型计算其下标。

注意事项

Java中实现了HashSet、HashMap、和LinkedHashSet和 LinkedHashMap
当我们使用Java中自带实现的HashMap和HashSet的时候一定要注意正确的使用hashCode(),另外还要正确的重写hashCode和equals方法。

LinkedHashSet和 LinkedHashMap,是链表 + 哈希表 组成
维护 element/key的有序,是按照插入顺序维护的。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无赖H4

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

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

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

打赏作者

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

抵扣说明:

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

余额充值