Java LinkedHashMap学习

Posted 学习、实践、总结

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java LinkedHashMap学习相关的知识,希望对你有一定的参考价值。

以前一直使用HashMap,今天学习一下LinkedHashMap

JavaDoc 注解:

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)

由Hash表和双向链表实现。有明确的遍历顺序,这个顺序一般是键插入的顺序。

另外,如果同样的key又插入了一遍,那么它还在原来的位置,不会受影响。

 


Map copy = new LinkedHashMap(m);   >>>This technique is particularly useful if a module takes a map on input, copies it, and later returns results whose order is determined by that of the copy. (Clients generally appreciate having things returned in the same order they were presented.)

这个方便特别有效果,如果一个模块需要使用一个入参map,拷贝map,而且复制品的key的顺序与入参map一样。(客户一般会欣赏:返回一个与给定的顺序一样的复制品)


以上是关于Java LinkedHashMap学习的主要内容,如果未能解决你的问题,请参考以下文章

Java LinkedHashMap学习

深入Java集合学习系列:LinkedHashMap的实现原理

Java集合类源码解析:LinkedHashMap

Java学习之LinkedHashMap

Java LinkedHashMap工作原理及实现

转:Java集合源码剖析LinkedHashmap源码剖析