14

For a map like:

Map<Integer, Integer> map = ...;
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
map.put(4, 4);

Is this code...

for (Integer i : map.keySet()) System.out.println(i);
for (Integer i : map.values()) System.out.println(i);

...guaranteed print the same same sequence twice?

If not, are there any guarantees in for example java.util.HashMap?

dacwe
  • 43,066
  • 12
  • 116
  • 140
  • possible duplicate of [is the Java HashMap keySet() iteration order consistent?](http://stackoverflow.com/questions/1882762/is-the-java-hashmap-keyset-iteration-order-consistent) – assylias Sep 05 '12 at 17:50
  • @assylias: This question is regarding key sets vs values. – dacwe Sep 05 '12 at 17:51
  • @daxwe is your question: will the 2 loops print the same thing if they are called twice, or is your question: will the loop over the keys and over the values print the corresponding key/value in the same order? – assylias Sep 05 '12 at 17:53
  • The currently chosen answer by parsifal is wrong. – Roland Aug 19 '16 at 08:07

3 Answers3

17

No, there is no guarantee, although in practice it will happen (there's no good reason for the map to use a different iterator for the keys and values).

If you want to guarantee iteration order, iterate the entrySet():

for (Map.Entry<Integer,Integer> entry : map.entrySet())
    // ...

Since you ask about HashMap, note also that any changes to the map will potentially change iteration order, as a result of the mapbeing rehashed.

parsifal
  • 582
  • 3
  • 4
  • I just read the [Map javadoc](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#entrySet--) and there is no order guarantee for `entrySet()`. Where did you get that from? – Roland Aug 19 '16 at 08:00
  • @Roland The goal is to get a key that is guaranteed to match the value. When doing two calls (keySet() and values()) there is no guarantee the both collections are in the same order. entrySet() guarantees this by definition. In general, there is no guaranty that two calls to entrySet will yield a collection in the same order (except for certain types like Linked... Tree...) – cquezel Jul 01 '20 at 19:55
7

No, not guaranteed. One is Set and one is Collection, neither guarantee the order.

If you would like to keep order. May be LinkedHashMap() with entrySet() help you.

kosa
  • 65,990
  • 13
  • 130
  • 167
-1

Yes. Sort of. You can use a subclass of SortedMap, i.e. TreeMap. That will keep keys in a natural order. (or you can give it a specific comparator). But when you use tree map, you HAVE to make sure the compareTo method "must be consistent with equals". Read the javadocs for more details. But in short, yes, you CAN sort a map.

JohnnyK
  • 1,103
  • 6
  • 10
  • The question was not whether you can sort a map, it was whether the set of keys returned from `keySet()`and the set of values returned from `valueSet()` will be ordered so that the n:th key in the key set corresponds to the n:th value in the value set. There is no such guarantee. There is also no guarantee that the order in which keys and values are returned from `keySet()` and `valueSet()` are even the same for two consecutive invocations. – JHH Sep 07 '17 at 19:38