guava/collections, how does one specify the iteration order in an ImmutableMap

687 views
Skip to first unread message

fishtoprecords

unread,
Apr 1, 2010, 6:54:41 PM4/1/10
to guava-discuss
From the javadocs:
An immutable, hash-based Map with reliable user-specified iteration
order.

This is just what I want. How do I specify the iteration order? I
don't see anywhere to specify a Comparator or similar thing.

Thanks
Pat

Kevin Bourrillion

unread,
Apr 1, 2010, 6:59:10 PM4/1/10
to guava-...@googlegroups.com
Eep.

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.  If a Comparator is choosing the order instead, that's actually an ImmutableSortedMap.

HTH.



--
guava-...@googlegroups.com.
http://groups.google.com/group/guava-discuss?hl=en
unsubscribe: guava-discus...@googlegroups.com

This list is for discussion; for help, post to Stack Overflow instead:
http://stackoverflow.com/questions/ask
Use the tag "guava".

To unsubscribe, reply using "remove me" as the subject.



--
Kevin Bourrillion @ Google
internal:  http://goto/javalibraries
external: http://guava-libraries.googlecode.com

Pat Farrell

unread,
Apr 1, 2010, 7:04:21 PM4/1/10
to guava-...@googlegroups.com
On Thu, Apr 1, 2010 at 6:59 PM, Kevin Bourrillion <kev...@google.com> wrote:
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.  If a Comparator is choosing the order instead, that's actually an ImmutableSortedMap.


OK, so the order its put in, that's an order I can control.
I assume I need to call a suitable Builder in the order I want, rather than just dumping in a HashMap for an ImmutableCopy.


Kevin Bourrillion

unread,
Apr 1, 2010, 7:15:24 PM4/1/10
to guava-...@googlegroups.com
Exactly, ImmutableMap.Builder.  Tiny maps (up to five entries) can use ImmutableMap.of().


--
guava-...@googlegroups.com.
http://groups.google.com/group/guava-discuss?hl=en
unsubscribe: guava-discus...@googlegroups.com
 
This list is for discussion; for help, post to Stack Overflow instead:
http://stackoverflow.com/questions/ask
Use the tag "guava".

Jared Levy

unread,
Apr 1, 2010, 7:18:18 PM4/1/10
to guava-...@googlegroups.com
On Thu, Apr 1, 2010 at 4:04 PM, Pat Farrell <pat2...@gmail.com> wrote:
You can also copy a TreeMap or LinkedHashMap that have the desired order. 
Reply all
Reply to author
Forward
0 new messages