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."
Related
LinkedHashMap is clearly an ordered Map. It orders based upon insertion.
So why does it not implement SortedMap?
From Java docs
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).
While sorted map is
A Map that further provides a total ordering on its keys. The map is
ordered according to the natural ordering of its keys, or by a
Comparator typically provided at sorted map creation time.
So Boths exists for different purpose Where LinkedHashMap provides iteration in the same order of key insertion while SortedMap is for sorting using Comparator or Comparable
LinkedHashMap delegates all the calls to its parent HashMap. it also supports access-ordering and insertion-ordering based on value of accessOrder.
if accessOrder : true then access order is used
else accessOrder :false insertion order is used
public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
but how is access-ordering and insertion-ordering of the objects internally supported by LinkedHashMap?
LinkedHashMap is 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). Note that insertion order is not affected if a key is re-inserted into the map.
http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html
The source code for the OpenJDK 6 version of LinkedHashMap is available here. Basically, the implementation keeps a doubly-linked list of all of the entries, and the entries' order is simply their place in the list.
I can't confirm this in the documentation but if i have a LinkedHashMap and i call keySet() on it and iterate over this set is it guaranteed to iterate in insertion order?
It's specified in the Map documentation:
The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.
That means for LinkedHashMap, all the 3 methods - values(), keySet() and entrySet(), each of them providing 3 different collection views, are guaranteed to iterate in the insertion order.
Yes.
See the docs(that you can not see) here: http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html
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). Note that insertion order is not affected if a key
is re-inserted into the map. (A key k is reinserted into a map m if
m.put(k, v) is invoked when m.containsKey(k) would return true
immediately prior to the invocation.)
I need a key-value pair data structure which guarantees the retrieval of entries in the order in which they were added, much like ArrayList or Vector for just singular entries. Think of it as an ArrayList that enables key-value pairs. Keep in mind, the TreeMap will not do because the sorting does not go by the value of the key but by the time of insertion. Is there a Java Collection that meets these requirements? I browsed different Map implementations but couldn't find any that match.
I understand I can define my class that takes the key and the value and put it in an ArrayList but that is only option B to a class described above.
Are you looking for LinkedHashMap ?
Hash table and linked list implementation of the Map interface, with predictable iteration order.
You can also look into the Guava's ImmutableMap if it suits your purpose.
An immutable, hash-based Map with reliable user-specified iteration order. Does not permit null keys or values.
I need a key-value pair data structure which guarantees the retrieval of entries in the order in which they were added,
LinkedHashMap
Hash table and linked list implementation of the Map interface, with predictable iteration order.
Use java.util.LinkedHashMap Javadocs here
"This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order)"
The JDK documentation on java.util.Map states
The order of a map is defined as the
order in which the iterators on the
map's collection views return their
elements. Some map implementations,
like the TreeMap class, make specific
guarantees as to their order; others,
like the HashMap class, do not.
And the documentation on TreeMap states:
The map is sorted according to the
natural ordering of its keys, or by a
Comparator provided at map creation
time, depending on which constructor
is used.
Is there any JDK implementation of Map that uses the order that elements were inserted into the map as the order?
java.util.LinkedHashMap:
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). Note that insertion order is not affected if a key is re-inserted into the map.