29

I need combination of Google Collection ImmutableMap and LinkedHashMap — immutable map with defined iteration order. It seems that ImmutableMap itself actually has defined iteration order, at least its documentation says:

An immutable, hash-based Map with reliable user-specified iteration order.

However there are no more details. Quick test shows that this might be true, but I want to make sure.

My question is: can I rely on iteration order of ImmutableMap? If I do ImmutableMap.copyOf(linkedHashMap), will it have same iteration order as original linked hash map? What about immutable maps created by builder? Some link to authoritative answer would help, since Google didn't find anything useful. (And no, links to the sources don't count).

1
  • I believe Collections has now been folded into the Guava library. But the documentation for ImmutableMap there is the same. Commented Sep 28, 2010 at 8:06

3 Answers 3

31

To be more precise, the ImmutableMap factory methods and builder return instances that follow the iteration order of the inputs provided when the map in constructed. However, an ImmutableSortedMap, which is a subclass of ImmutableMap. sorts the keys.

Sign up to request clarification or add additional context in comments.

Comments

28

I've actually found discussion about this, with answers from library authors:

Kevin Bourrillion: What we mean by "user-specified" is "it can be whatever order you want it to be"; in other words, whatever order you provide the entries to us in the first place, that's the order we use.

Jared Levy: You can also copy a TreeMap or LinkedHashMap that have the desired order.

Yes, I should have believed the javadoc, although I think that javadoc can be better in this case. It seems I'm not first who was confused by it. If nothing else, this Q/A will help Google next time someone searches for "ImmutableMap iteration" :-)

2 Comments

+1 I agree with you that the JavaDoc could be more clear. There is probably no other interpretation for "reliable user-specified iteration order", but a little redundant remark on the copyOf() method that it keeps the iteration order of the source map would not have hurt. The of() family of methods does have this sort of comment ("Returns an immutable map containing the given entries, in order.")
@Thilo: ah, I haven't noticed these comments for of() methods. Thanks.
6

You should believe the javadoc. If it is not enough, read the source code or report the bug.

A quick view to the source code shows that the map is backed by array and iteration will be done through ImmutableSet that is also backed by an array. So I think the documentation is correct and the order of the elements will be kept as it is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.