javahashmaplinkedhashmap

Iteration order of LinkedHashMap collection views


After reading the documentation for LinkedHashMap (and having used it several times), I'm still not clear about one of its properties. Is the iteration order for a LinkedHashMap:

  1. the same as insertion order for entrySet(), keySet(), and values(), or
  2. the same as insertion order for entrySet() and keySet() but not values(), or
  3. only the same as insertion order for entrySet()?

I imagine the third scenario to be unlikely, but I would like to know if anyone knows if (1) or (2) is true since iteration over values() is probably a rare use case.


Solution

  • LinkedHashMap respects insertion order; so the first choice is the good.

    A Map being a set of Map.Entry objects, options 2 and 3 would be rather strange ;)