4

I know the difference between all of them and I understand that LinkedHashMap and LinkedHashSet provide an insertion-ordering. I understand that LinkedHashMap extends HashMap and LinkedHashSet extends HashSet.

Why don't we always use LinkedHashMap instead of HashMap and why don't we always use LinkedHashSet instead of HashSet?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
O Connor
  • 4,236
  • 15
  • 50
  • 91
  • This is primarily opinion based. IMO the reason is that it depends on what you want/need: if you need to store key/value pair and you don't worry about the order, use `HashMap`, if you need to maintain the order of the insertion of the elements, use `LinkedHashMap`, similar with `Set` implementations. A real world example of this may be using JSF, when filling the data for `` in view, `HashMap` will set the data depending on the order of the `hashCode`s of the keys, while it will be *ordered* if using a `LinkedHashMap`, note that this changes since JSf 2 because it allows `List` – Luiggi Mendoza Jun 20 '14 at 16:14
  • [Good resource](http://stackoverflow.com/questions/5080612/hashset-vs-linkedhashset) to refer to. – But I'm Not A Wrapper Class Jun 20 '14 at 16:14

3 Answers3

9

Keeping the insertion order has its associated costs, both in terms of needing more memory, and spending additional CPU cycles:

  • You need additional memory to keep the extra links,
  • You need additional CPU cycles to maintain it.

Although the asymptotic complexity is the same, the added convenience does not come for free. If you do not need the insertion order maintained, you do not have to "pay" for it, and use lighter-weight HashSet<E> and HashMap<K,V> instead.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    For example, you can see the per-element memory costs of each of the basic Java data structures [here](https://code.google.com/p/memory-measurer/wiki/ElementCostInDataStructures), and see how much overhead the linked list requires. – Louis Wasserman Jun 20 '14 at 17:57
2

Ordering is brought at the cost of efficiency in LinkedhashMap / LinkedHashSet. So, whenever we don't need Ordering, we could use hashMap / HashSet.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

The other answers about cost are correct, but just to clarify why that's important (This would be a better comment but it's going to be too long.)

Java is, believe it or not, a very low-level language as far as CPU goes. It does hog up a bit of memory, but if you use it correctly it can perform very close to C (typically 1/2 the speed, sometimes nearly 1:1 depending on the task). This is as opposed to languages like Python which is quite quick at 1/10 the speed of C and Ruby which is not that qhick (1/50-1/100 the speed of C)

In terms of ease of use though, Java can be used as a very high-level language, implementing huge web solutions in Java alone.

For most of the high-level solutions your observation is quite accurate--the LinkedHashMap would have virtually no impact over a HashMap, but for low-level, fast running apps (On a small scale think MineCraft real-time graphics--or on a large scale consider a distributed web cluster at Google). In these cases you can program as though you were writing in C. You eliminate memory allocations and write very close to the metal to get every bit of speed.

Java tries very hard to always allow the "Fast" solution, that's one of the biggest differences betwen Java and many of the later generation languages--Every little bit of java is optimized (or at least optimizable) wherever possible.

As a concrete example--twitter was originally implemented in Ruby on Rails. When it comes down to it, the lanuge just couldn't be made fast enough (Although it was probably the reason for Twitters successs--being easier to get it up and running in the first place!!). When they replaced ROR with Java they were able to eliminate 9/10 of their servers and have better throughput.

Bill K
  • 62,186
  • 18
  • 105
  • 157