문제

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