+3  A: 

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.

nanda
+5  A: 

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" :-)

Peter Štibraný
+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
@Thilo: ah, I haven't noticed these comments for of() methods. Thanks.
Peter Štibraný
+2  A: 

Others have answered your question correctly, but I want to add some general advice related to your question:

When I am not 100% sure of the behavior of a library, or when I fear a change of behavior in a future release, I write a unit test to verify my assumptions.

That way, when upgrading, I will be notified of the problem, if the behavior has indeed changed.

Writing unit tests is also useful when "discovering" a library.

eneveu
Thanks for the tip. Yes, I wrote small test to check this behavior. But I also wanted to clear up the original intent. Unfortunately application where I want to use this doesn't have unit test suite that would be run after each build.
Peter Štibraný
Yes. It was more of a general suggestion for this class of problems (I think Guava already has unit tests to verify this). Sadly, many apps do not have unit test suites... To clarify: I think your question was legitimate, and the javadocs might be improved by better explaining the iteration order behavior of these maps.
eneveu
+2  A: 

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.

Jared Levy