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.)
Related
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).
I have a HashMap.
It has 100s of millions of observations.
What's the best way to iterate over the elements of the HashMap, in numerical order of the keys?
I considered changing to TreeMap, but did not do that since it may actually increase the load in creating the Map (as TreeMap is O(n), HashMap is O(1)).
With Java 8 you could use something similar to the following:
import static java.util.Comparator.comparing;
map.entrySet().stream()
.sorted(comparing(Entry::getKey))
.forEach(e -> doSomethingWithTheEntry(e));
That will obviously involve sorting the unsorted keys, which will come at a cost. So you need to decide whether you want to pay the cost upfront with a TreeMap or when required and keep using a HashMap.
You can't iterate over a HashMap in order. You'll have to use TreeMap for that. If you use a LinkedHashMap, you can iterate in the order the keys were inserted to the Map, but it's still not what you want (unless you insert the keys in numerical order).
If your insertion order is the same order as your keys, then you could use a 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). 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.)
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.
Which collections can I use if I want to put employee details 1)as key value 2) without key value. In both case I want to retrieve data in the order in which they are inserted.Please Help.
For simple List implementations (simple value list storage), you can use the ArrayList class. For Map (key-value storage), use LinkedHashMap. Both of these implementations will preserve insertion order.
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). 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.)
For Key-Value pair in ordered format: LinkedHashMap
For non - Key-Value pair in ordered format: ArrayList
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.