首页 > 代码库 > Java LinkedHashMap学习

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学习