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.
Ordering is brought at the cost of efficiency in LinkedhashMap / LinkedHashSet. So, whenever we don't need Ordering, we could use hashMap / HashSet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With