class LinkedHashMap<KeyType, ValueType>
LinkedHashMap
is the same as a HashMap
, except that it exposes its key/value pairs in the order they were inserted, rather than in the unpredictable order of a standard HashMap
.
This is accomplished by storing the data in two ways internally: (1) in a hash table array, as in a HashMap
; and (2) in a linear list of keys, to remember the insertion order.
A LinkedHashMap
provides identical lookup (containsKey
/ get
/ operator []
) speed, nearly the same insertion (add
/ put
) speed, and slower removal (remove
) speed.
Its for-each loop and iterators emit the keys in the order they were added to the map, at the cost of the additional memory usage of the internal list.
Since the members of LinkedHashMap
are the same as those of HashMap
, in the interest of avoiding redundancy, we refer you to the HashMap
documentation for descriptions of each member.
The only noteworthy difference in the documentation is that the operator []
of a LinkedHashMap
is currently read-only; it cannot be used for insertion.
To insert into a LinkedHashMap
, use the put
or add
methods.