90

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?

3

3 Answers 3

58

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.

5
  • 15
    but entrySet returns a Set which is unordered itself?
    – Jonathan.
    Commented Dec 7, 2014 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
    Commented Feb 12, 2016 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 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
    Commented Jun 2, 2016 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
    Commented Jun 2, 2016 at 19:34
  • 8
    Found a definitive answer here: 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
    Commented Jun 2, 2016 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.

1
  • 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
    Commented Jun 2, 2016 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.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.