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.)
Related
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.)
Let's say I put keys "a", "b", "c" into a TreeMap. How would I use "c" to determine that it was the third element in lexicographic order in the TreeMap?
What is the purpose of the NavigableSet obtained from a TreeMap?
Use LinkedHashMap if you want to retrieve the element in the order in which you insert.
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.)
If what you actually want to know is the index of the c key in the sorted set of keys contained in the map, you could simply use
int index = map.headMap("c").size();
Note that it doesn't have anything to do with insertion order, which a TreeMap doesn't maintain.
Is LinkedHashMap LIFO or FIFO in nature?
If my map is of the form:
map.put(1,"one");
map.put(2,"two");
what would be the order if I was to iterate on the map using keyset??
EDIT: I think I did actually confuse two different concepts. Let me rephrase the question. What would be the order in which I encounter the quantities using entryset?Thanks for pointing that out btw. I do not intend to remove any entry.
In a linked hash map the elements in the backing doubly-linked list are added at the end (clearly: for preserving iteration order), but can be removed from any part in the list as the elements get removed from the map, it's incorrect to label the backing list (and by extension: the map) as LIFO or FIFO, it's neither - there's no concept of removal order in a map, and consequently no removal order can be assumed for the backing list in a linked hash map.
What a linked hash map does guarantee is that iterating over its contents (be it: the keys or the entries) will occur in the same order in which the elements were inserted in the map; from the documentation:
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).
EDIT :
Regarding the last edit to the question, a LinkedHashMap guarantees that the iteration order of the keySet() will be the same order in which the elements were inserted: 1, 2 for the example in the question. This has nothing to do with FIFO/LIFO, those concepts deal with the order in which elements are removed from a data structure, and they're not related with the iteration order after inserting elements.
LinkedHashMap to quote from the javadocs is "Hash table and linked list implementation of the Map interface, with predictable iteration order" . So the keySet will return keys based on the order of insertion, esssentially a FIFO.
When access order is not utilized (standard case) you can consider LHM as a linked list w/ very fast access O(1) by key.
In that aspect it is FIFO when access order is unused (look at the c-tors). When access order is used the insertion order doesn't matter if there are any get() operations as they reorder the Entries. Look at protected boolean removeEldestEntry(Map.Entry<K,V> eldest) eldest=FIFO.
Essentially the LHM is a good doubly linked list of Map.Entry<Key, Value> with a hash index over the keys.
I myself never use the vanilla HashMap as in its current impl. it has very little benefit over LHM - lower memory footprint but horrid iteration. Java8 (or 9) perhaps may finally fix HashMap, hopefully Doug Lea will push his impl.
According to Java docs, if you were to iterate over the map, the keyset would be in insertion-order. So the first key you get is the first key entered, over the existing keys. Note, reinserting a key-value pair does not change the original key position.
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
I am using a linkedHashMap to guarantee order when someone tries to access it. However, when it comes time to iterate over it, does using entrySet() to return key/value pairs guarantee order as well? No changes will be made while iterating.
EDIT: Also, are there any adverse effects from iterating through the map by iterating through its keys and calling get?
According to the Javadocs, yes.
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).
As for the edit, no, it should work just fine. But the entry set is somewhat faster since it avoids the overhead of looking up every key in the map during iteration.
If you're sure no changes will be made during the iteration, then proper ordering with entrySet() is guaranteed, as stated in the API.
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.)