Using the new collections from Google's Guava, http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained
How do I loop over a MultiMap for each key in the order of insertion?
For example
multimap = new HashMultiMap<String,String>();
multimap.put("1", "value1");
multimap.put("1", "value2");
multimap.put("1", "value3");
multimap.put("2", "value11");
multimap.put("2", "value22");
multimap.put("2", "value33");
multimap.put("3", "value111");
multimap.put("3", "value222");
multimap.put("3", "value333");
On each loop I need
"value1", "value11", "value111";
then the next loop
"value2", "value22", "value222";
and so on:
"value3", "value33", "value333";
I'm not quite sure what are your needs (or concrete use case), but I'll try to guess. Other answers suggest using Linked*Multimap or Immutable one, but to get desired output (shown in question) with Multimap
you will have to create some fancy Map (I'll discuss this later) or for example create three temporary collections holding first, second and third values for each key (they will be in insertion order, if you use one of suggested Multimap
implementations). Preferably it would be one of ListMultimaps
as you can iterate over multimap.keySet()
to get lists with values available by index:
final ListMultimap<String,String> multimap = LinkedListMultimap.create();
// put values from question here
final List<Object> firstValues = Lists.newArrayList();
for (final String key: multimap.keySet()) {
firstValues.add(multimap.get(key).get(0));
}
System.out.println(firstValues);
// prints [value1, value11, value111]
// similar for multimap.get(key).get(1) and so on
but the downside is that you'll have to create three Lists for you example what makes this solution rather unflexible. So maybe it'll be better to put {first,second,third}Values collection to Map>, what brings me to the point:
Maybe you should use Table instead?
Table is designed as A collection that associates an ordered pair of keys, called a row key and a column key, with a single value and, what's more important here, has row and column views. I'll use ArrayTable
here:
final ArrayTable<String, Integer, Object> table = ArrayTable.create(
ImmutableList.of("1", "2", "3"), ImmutableList.of(0, 1, 2));
table.put("1", 0, "value1");
table.put("1", 1, "value2");
table.put("1", 2, "value3");
table.put("2", 0, "value11");
table.put("2", 1, "value22");
table.put("2", 2, "value33");
table.put("3", 0, "value111");
table.put("3", 1, "value222");
table.put("3", 2, "value333");
for (final Integer columnKey : table.columnKeyList()) {
System.out.println(table.column(columnKey).values());
}
// prints:
// [value1, value11, value111]
// [value2, value22, value222]
// [value3, value33, value333]
I deliberately used String for row keys which are [1, 2, 3, ...] Integers in fact (like you did in the question) and Integers for column keys starting with 0 ([0, 1, 2, ...]) to show similarity to previous example using List's get(int)
on multimaps values' collection.
Hope this will be helpful, mostly in determining what you want ;)
P.S. I use ArrayTable
here, because it has neater way of creating fixed set (universe) of rows / keys values than ImmutableTable
, but if mutability isn't required you should use it instead with one change - ImmutableTable
(and any other Table implementation) doesn't have columnKeyList()
method, but only columnKeySet()
which does the same thing, but is slower for ArrayTable
. And of course ImmutableTable.Builder
or ImmutableTable.copyOf(Table)
should be used.
You can use either LinkedListMultimap
or LinkedHashMultimap
.
The two have very similar behavior; one major difference is that LinkedListMultimap
allows multiple copies of the same key-value pair to be inserted, whereas LinkedHashMultimap
allows only one.
See the above-linked Javadoc for much more information.
I'm not entirely clear what iteration order you mean, OP...
Set<K> keySet()
just returns the keys.Map<K, Collection<V>> asMap()
returns the keys and their associated entries, so you might do for (Map.Entry<K, Collection<V>> entry : asMap().entrySet())
to iterate over keys and their associated collections.Collection<Map.Entry<K, V>> entries()
lets you iterate over the entries, but without necessarily grouping them by key.If you want things in insertion order, use one of the insertion-ordered Multimap
implementations -- LinkedHashMultimap
, possibly LinkedListMultimap
, ImmutableMultimap
.
For looping over multiple keys:
for (Object key : multimap.keys()) { ... }
You can also loop over the entries:
for (Map.Entry entry : multimap.entries()) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With