LinkedHashMap vs. HashMap?
-
A
LinkedHashMap differs from HashMap in that the
order of elements is maintained.
-
A
HashMap has a better performance than a LinkedHashMap
because a LinkedHashMap needs the expense of maintaining the
linked list. The LinkedHashMap implements a normal hashtable, but
with the added benefit of the keys of the hashtable being stored as a
doubly-linked list.
-
Both of their methods are not synchronized.
Let's take a look their API documentations:
The HashMap is a hash table with buckets in each hash slot. Like in the API
documentation:
This implementation provides constant-time performance for the basic operations
(get and put), assuming the hash function disperses the elements properly among
the buckets. Iteration over collection views requires time proportional to the
"capacity" of the HashMap instance (the number of buckets) plus its size (the
number of key-value mappings). Thus, it's very important not to set the initial
capacity too high (or the load factor too low) if iteration performance is
important.
The LinkedHashMap is a linked list implementing the map interface. As said in
the API documentation:
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).