Question

Why is LinkedHashMap is ordered? If it implements the Map interface and extends AbstractMap and HashMap, Which of them provides the ordering feature?

Was it helpful?

Solution

From the documentation of LinkedHashMap

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

So the ordering comes from the implementation of LinkedHashMap and not from extending java.util.AbstractMap<K,V> or java.util.HashMap<K,V>

OTHER TIPS

LinkedHashMap is ordered because that is part of its contract. See http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html, for example "Hash table and linked list implementation of the Map interface, with predictable iteration order.".

It also states how it is different from HashMap: "This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top