之前一直使用HashMap,今天學習一下LinkedHashMapapp
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.)spa
由Hash表和雙向鏈表實現。有明確的遍歷順序,這個順序通常是鍵插入的順序。orm
另外,若是一樣的key又插入了一遍,那麼它還在原來的位置,不會受影響。ci
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.)input
這個方便特別有效果,若是一個模塊須要使用一個入參map,拷貝map,並且複製品的key的順序與入參map同樣。(客戶通常會欣賞:返回一個與給定的順序同樣的複製品)it