Java Linkedhashmap 如何维护插入顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20171999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:43:03  来源:igfitidea点击:

How linkedhashmap maintains insertion order

javahashmap

提问by Newbie

I know how Hashmap works internally. Linkedhashmap is extending Hashmap class. So how Linkedhashmap is able to maintain the insertion order. I have read the javadoc for Linkedhashmap, but it does not have any details about this. Can somebody help me understand this?

我知道 Hashmap 在内部是如何工作的。Linkedhashmap 是对 Hashmap 类的扩展。那么 Linkedhashmap 是如何维护插入顺序的。我已经阅读了 Linkedhashmap 的 javadoc,但它没有关于此的任何详细信息。有人可以帮助我理解这一点吗?

Thanks in advance.

提前致谢。

回答by Daber

http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html.

http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html

Idea behind implementation is quite simple. It extends regular hashMap (so it has all hashMap goodies) but also builds double linked list when adding elements.

实现背后的想法非常简单。它扩展了常规 hashMap(因此它具有所有 hashMap 优点),但在添加元素时也会构建双链表。

(entries are also extended from the HashMap.Entry so they have pointers to after and before) So all entries are ordered HEAD -> Entry1 <-> Entry2 ... <-- TAIL

(条目也是从 HashMap.Entry 扩展而来的,所以它们有指向之后和之前的指针)所以所有条目的顺序都是 HEAD -> Entry1 <-> Entry2 ... <-- TAIL

and at the same time kept in standard HashSet (i assume you are familiar with implementation).

并同时保存在标准 HashSet 中(我假设您熟悉实现)。

Now when iterating It Linked list of entries is used.

现在在迭代它时使用链接的条目列表。

回答by fscore

It maintains a linkedlist of the entries in the map in the order in which they were inserted. This helps to maintain iteration order and elements will be returned in the order they were first added in.

它按照插入顺序维护映射中条目的链表。这有助于保持迭代顺序,元素将按照它们第一次添加的顺序返回。

You would like reading this post too as whenever you start comparing, you might understand better: Difference between HashMap, LinkedHashMap and TreeMap

您也想阅读这篇文章,因为每当您开始比较时,您可能会更好地理解:HashMap、LinkedHashMap 和 TreeMap 的区别

回答by Java Strikers

Internally it maintains double linked list (Map.Entry) to store objects in the order , because double linked list stores the address of previous node and next node .

它内部维护双链表(Map.Entry)来按顺序存储对象,因为双链表存储的是上一个节点和下一个节点的地址。

Same you can also check in source code .

同样,您也可以签入源代码。

回答by Avi Tyagi

The reference fields before and after maintains a doubly linked list for all the entriesof the LinkedHashMap. Using before and after fields, we can traverse all the entries of a LinkedHashMap.
Internal of LinkedHashMap : http://techmastertutorial.in/java-collection-internal-linked-hashmap.html

参考字段之前和之后保持一个双向链表的所有条目中的LinkedHashMap的。使用 before 和 after 字段,我们可以遍历LinkedHashMap 的所有条目。
LinkedHashMap 内部:http: //techmastertutorial.in/java-collection-internal-linked-hashmap.html