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?

2
  • 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 <f:selectItems> in view, HashMap will set the data depending on the order of the hashCodes of the keys, while it will be ordered if using a LinkedHashMap, note that this changes since JSf 2 because it allows List Commented Jun 20, 2014 at 16:14
  • Good resource to refer to. Commented Jun 20, 2014 at 16:14

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

For example, you can see the per-element memory costs of each of the basic Java data structures here, and see how much overhead the linked list requires.
2

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.