Engineering Full Stack Apps with Java and JavaScript
LinkedHashMap extends HashMap and has a predictable iteration order.
LinkedHashMap implementation maintains a doubly-linked list running through all of its entries.
The doubly linked list maintained by the LinkedHashMap implementation defines the iteration ordering.
By default, the order is the order in which keys were inserted into the map.
The insertion order is not affected if a key is re-inserted into the map.
LinkedHashMap implementation provides ordering without incurring the increased cost associated with TreeMap.
LinkedHashMap can be used to produce a copy of a map that has the same order as the original, regardless of the original map's implementation:
Map copy = new LinkedHashMap(m);
A special constructor is provided to create a linked hash map whose order of iteration is the order in which its entries were last accessed, from least-recently accessed to most-recently (access-order). This kind of map is well-suited to building LRU caches.
Following methods generate entry access, no other methods generate entry accesses:
Invoking the put, putIfAbsent, get, getOrDefault, compute, computeIfAbsent, computeIfPresent, or merge methods results in an access to the corresponding entry.
The replace methods only result in an access of the entry if the value is replaced.
The putAll method generates one entry access for each mapping in the specified map, in the order that key-value mappings are provided by the specified map's entry set iterator.
Operations on collection-views do not affect the order of iteration of the backing map.
The removeEldestEntry(Map.Entry) method may be overridden to impose a policy for removing stale mappings automatically when new mappings are added to the map.
LinkedHashMap implementation provides all of the optional Map operations, and permits null elements.
Like HashMap, it provides constant-time performance for the basic operations (add, contains and remove), assuming the hash function disperses elements properly among the buckets.
Performance is likely to be just slightly below that of HashMap, due to the added expense of maintaining the linked list.
However, iteration over the collection-views of a LinkedHashMap requires time proportional to the size of the map, regardless of its capacity.
Iteration over a HashMap is likely to be more expensive, requiring time proportional to its capacity.
A linked hash map has two parameters that affect its performance: initial capacity and load factor. They are defined precisely as for HashMap.
However, that the penalty for choosing an excessively high value for initial capacity is less severe for LinkedHashMap than for HashMap, as iteration times for this class are unaffected by capacity.
LinkedHashMap implementation is not synchronized.
The iterators returned by the iterator method of the collections returned by all of this class's collection view methods are fail-fast.
However, the fail-fast behavior of an iterator cannot be guaranteed. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis.
The spliterators returned by the spliterator method of the collections returned by all of this class's collection view methods are late-binding, fail-fast, and additionally report Spliterator.ORDERED.
For the complete list of methods and constructors, refer to Oracle documentation for Java (given in reference).