87

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?

Diego
  • 16,830
  • 8
  • 34
  • 46
  • I was about to ask the same question, but why ask it if someone else already asked and received a good answer? +1 for a good question. – uTubeFan Sep 19 '12 at 18:50
  • In LinkedHashMap you can use insert-order and access-order. The order is maintained always. Please cover my [Internal life of LinkedHashMap](http://volodial.blogspot.com/2013/07/internal-life-of-linkedhashmap-in-java.html) tutorial – Volodymyr Levytskyi Jul 30 '13 at 08:46
  • Possible duplicate of [Is the order guaranteed for the return of keys and values from a LinkedHashMap object?](http://stackoverflow.com/questions/2923856/is-the-order-guaranteed-for-the-return-of-keys-and-values-from-a-linkedhashmap-o) – Roland Mar 13 '17 at 10:09

3 Answers3

57

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.

David Harkness
  • 35,992
  • 10
  • 112
  • 134
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • 15
    but entrySet returns a Set which is unordered itself? – Jonathan. Dec 07 '14 at 13:28
  • 6
    Yes, technically the entrySet itself has no way to access access a position, but entrySet.iterator() does. The iterator lets us have an ordered list. – Jpatrick Feb 12 '16 at 20:30
  • 3
    I would like to know that this answer is correct, but as Jonathan pointed out, `entrySet()` returns a Set. Jpatrick says the set's iterator lets us have an ordered list, but http://docs.oracle.com/javase/6/docs/api/java/util/Set.html#iterator() says "The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee)." And none of the docs referenced here say that entrySet() returns a Set that's an instance of a class that guarantees order. Am I missing something? – LarsH Jun 02 '16 at 19:31
  • 1
    Michael Myers is right to point out the documentation that says the LinkedHashMap defines an iteration ordering, but it doesn't say how to access that ordering. `entrySet().iterator()` is a plausible way, but can it be relied upon? – LarsH Jun 02 '16 at 19:34
  • 6
    Found a definitive answer here: http://stackoverflow.com/a/2924143/423105 "The order of a map is defined as the order in which the iterators on the map's collection views return their elements." -- javadoc for `Map` – LarsH Jun 02 '16 at 19:39
2

If you're sure no changes will be made during the iteration, then proper ordering with entrySet() is guaranteed, as stated in the API.

Daniel F. Thornton
  • 3,687
  • 2
  • 28
  • 41
  • 3
    The API docs that you linked to for `entrySet()` don't say anything about guaranteeing proper ordering. The only related statement on that page is "This class [i.e. HashMap] makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time." What were you referring to with that link? – LarsH Jun 02 '16 at 19:21
1

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

Robert
  • 8,406
  • 9
  • 38
  • 57