Implementation of java.util.Map using insertion order as order - java

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.

Related

How to add keys from a HashMap into a list with the same order in Java

I've used a HashMap to order keys out from their values. Now I want the keys transfered over to a list, where the order of the keys will be the same. I've tried multiple option to add each key to a list, but none of them keeps the same order of the keys, as they had in the HashMap.
HashMap is the most known and common implementation of Map. However:
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
If you want to persist the order in which the elements have been put to the Map, you need to use LinkedHashMap implementation. As the documentation states:
This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

Why does LinkedHashMap not implement SortedMap?

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

Why LinkedHashMap is ordered?

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

How does LinkedHashMap internally preserves the insertion order of objects

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.

LinkedHashMap iterating over the keys

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

Categories