LinkedHashMap的内部实现与HashMap实现有什么不同? [英] How is the internal implementation of LinkedHashMap different from HashMap implementation?

查看:98
本文介绍了LinkedHashMap的内部实现与HashMap实现有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过HashMap的实现:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' →入口→入口←链表实现
[入口]
[入口]→入口
[入口]
[null]



因此,它有一个Entry对象数组。



问题:


  1. 我在想,如果这个数组的索引在同一个hashCode但是不同的对象的情况下存储多个Entry对象,它该如何存储。


  2. 这与 LinkedHashMap 实现有何不同?它的映射的双向链表实现,但它是否像上面那样维护一个数组,并且它如何存储指向下一个和上一个元素的指针?

因此,它有一个 Entry 对象的数组。

解决方案

不完全。它有一个 Entry 对象的数组。一个 HashMap.Entry 对象有一个下一个字段,允许 Entry
$ b


我想知道该数组的索引如何存储多个条目对象在相同的hashCode但不同的对象的情况下。


因为(如问题中的图片所示)条目对象被链接。


这与 LinkedHashMap 实现有何不同?它的映射的双向链表实现,但它是否像上面那样维护一个数组,并且它如何存储指向下一个和上一个元素的指针?



<在 LinkedHashMap 实现中, LinkedHashMap.Entry 类扩展了 HashMap.Entry 类,在和之后的字段中添加。这些字段用于将 LinkedHashMap.Entry 对象组合成独立的双向链接列表,用于记录插入顺序。因此,在 LinkedHashMap 类中,入口对象有两个不同的链:


  • 通过主散列数组访问的单链接哈希链,以及保存在条目中的所有条目的单独双向链接列表

  • 广告订单。

I read that HashMap has the following implementation:

main array 
   ↓
[Entry] → Entry → Entry      ← linked-list implementation
[Entry]
[Entry] → Entry
[Entry]
[null ]

So, it has an array of Entry objects.

Questions:

  1. I was wondering how can an index of this array store multiple Entry objects in case of same hashCode but different objects.

  2. How is this different from LinkedHashMap implementation? Its doubly linked list implementation of map but does it maintain an array like the above and how does it store pointers to the next and previous element?

解决方案

So, it has an array of Entry objects.

Not exactly. It has an array of Entry object chains. A HashMap.Entry object has a next field allowing the Entry objects to be chained as a linked list.

I was wondering how can an index of this array store multiple Entry objects in case of same hashCode but different objects.

Because (as the picture in your question shows) the Entry objects are chained.

How is this different from LinkedHashMap implementation? Its doubly linked list implementation of map but does it maintain an array like the above and how does it store pointers to the next and previous element?

In the LinkedHashMap implementation, the LinkedHashMap.Entry class extends the HashMap.Entry class, by adding before and after fields. These fields are used to assemble the LinkedHashMap.Entry objects into an independent doubly-linked list that records the insertion order. So, in the LinkedHashMap class, the entry objects are in two distinct chains:

  • a singly linked hash chain that is accessed via the main hash array, and

  • a separate doubly linked list of all entries that is kept in entry insertion order.

这篇关于LinkedHashMap的内部实现与HashMap实现有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆