LinkedHashMap中为什么需要hashcode和bucket

Why do we need hashcode and bucket in LinkedHashMap

最近,我一直在研究 javaMap 接口的实现。我理解 HashMap,一切都说得通了。 但是说到LinkedHashMap,据我所知,Entry有keyvaluebeforeafter。 before 和 after 跟踪插入顺序。

但是,在 LinkedHashMaps 中使用 hashcode 和桶概念对我来说没有意义。

I went through this article for understanding implementation of linkedHashMaps

有人可以解释一下吗?我的意思是为什么我们将入口节点放在哪个桶中很重要。事实上,为什么桶的概念放在首位。?为什么不使用简单的双向链表?

LinkedHashMap仍然是HashMap的一种。它使用与 HashMap 相同的逻辑来查找密钥所属的桶(用于 get()put()containsKey() 等方法...) . hashCode() 用于定位那个桶。此逻辑对于这些操作的预期 O(1) 性能至关重要。

LinkedHashMap 的新增功能(使用 beforeafter 引用)仅用于根据插入顺序迭代条目,因此它会影响Collections 由 keySet()entrySet()values() 方法返回。它不影响条目的存储位置。

如果没有哈希码和存储桶,LinkedHashMap 将无法在 O(1) 预期时间内在 Map 中查找键。