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

有帮助吗?

解决方案

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>

其他提示

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."

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top