posted 21 years ago
Every map has an iteration order that depends on the type of the map, for example, a HashMap has an unknown iteration order, but a TreeMap has an iteration order that depends on a comparator or on the method compareTo() of the object being inserted (if it implements java.lang.Comparable).
Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?
A LinkedHashMap has the fast feature of a HashTable and the ability to preserve the insertion order. A simple HashMap does not have this ability. addAll() method will be called, this method gets each object of one collection (based on the iteration order) and inserts in another collection. Since a LinkedHashMap preserves the insertion order, the order of the iteration with the objects of a linkedHashMap will be the same of the collection that you used to create this LinkedHashMap!