Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Collections ImmutableMap iteration order

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

like image 587
Peter Štibraný Avatar asked Sep 28 '10 07:09

Peter Štibraný


People also ask

Does ImmutableMap maintain 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.

What is ImmutableMap?

ImmutableMap, as suggested by the name, is a type of Map which is immutable. It means that the content of the map are fixed or constant after declaration, that is, they are read-only. If any attempt made to add, delete and update elements in the Map, UnsupportedOperationException is thrown.

Are Scala maps immutable?

There are two kinds of Maps, the immutable and the mutable. The difference between mutable and immutable objects is that when an object is immutable, the object itself can't be changed. By default, Scala uses the immutable Map.

How can we create immutable HashMap in Java?

We used to use the unmodifiableMap() method of Collections class to create unmodifiable(immutable) Map. Map<String,String> map = new HashMap<String, String>(); Map<String,String> immutableMap = Collections. unmodifiableMap(map);


2 Answers

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.

like image 191
Jared Levy Avatar answered Sep 23 '22 01:09

Jared Levy


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

like image 43
Peter Štibraný Avatar answered Sep 23 '22 01:09

Peter Štibraný