Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iteration order of a LinkedHashMap

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.

like image 380
Andrew Mao Avatar asked Jan 14 '13 20:01

Andrew Mao


1 Answers

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

like image 199
fge Avatar answered Oct 30 '22 00:10

fge