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
?
Keeping the insertion order has its associated costs, both in terms of needing more memory, and spending additional CPU cycles:
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.