3

Are there any disadvantages of using a LinkedHashMap instead of a HashMap? Most posts seem to discuss the advantages of LinkedHashMaps (such as this one or the API), but I can't find any reason HashMaps are better.

Community
  • 1
  • 1
JRoy
  • 33
  • 3
  • possible duplicate of [Difference between HashMap, LinkedHashMap and SortedMap in Java](http://stackoverflow.com/questions/2889777/difference-between-hashmap-linkedhashmap-and-sortedmap-in-java) – donfuxx Mar 23 '14 at 16:53
  • Disadvantage is pretty much listed in the linked question - performance. – nawfal Jun 30 '14 at 14:52

2 Answers2

9

As the docs say, This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries.. This has the benefit of allowing predictable iteration order, but the disadvantages are increased memory usage and probably higher insertion cost - nothing comes for free, the additional structure (linked list) uses some memory and requires extra CPU cost in order to be maintained.

Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
0

Yes there is. LinkedHashMap differs from HashMap in that the order of elements is maintained.

So in order to maintain order, LinkedHashMap needs the expense of maintaining a linked list. Whereas a HashMap has no such overhead leading to a better performance than a LinkedHashMap.

Note that LinkedHashMap implements a normal hashtable, but with the added benefit of the keys of the hashtable being stored as a doubly-linked list.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264