Java – how to iterate over a Multimap in insertion order?
Using guava from Google, http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained New series
How do I iterate through Multimap for each key in insertion order?
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 every cycle I need
"value1","value11","value111";
Then the next cycle
"value2","value22","value222";
wait:
"value3","value33","value333";
Solution
I'm not sure about your requirements (or specific use cases), but I'll guess Other answers suggest using linked * Multimap or immutable, but to use Multimap to get the required output (as shown in the figure), you will have to create some fancy maps (I will discuss later) or, for example, create three temporary collections, the first, the second and the third value of each key (if you use one of the proposed Multimap implementations, they will be arranged in insertion order) It's best to be one of the listmultimaps because you can iterate over the Multimap Keyset() to get the list of available values for the 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
The downside is that you have to create three lists for you, such as what makes this solution quite inflexible So maybe it would be better to put the {first, second, third} value set in Map >, which reminds me of this:
Maybe you should use table instead of?
A table is designed as a set, which associates a pair of ordered keys (called row keys and column keys) with a value, and here it is more important to have row and column views I use arraytable here:
final ArrayTable<String,Integer,Object> table = ArrayTable.create( ImmutableList.of("1","2","3"),ImmutableList.of(0,1,2)); table.put("1","value1"); table.put("1","value2"); table.put("1",2,"value3"); table.put("2","value11"); table.put("2","value22"); table.put("2","value33"); table.put("3","value111"); table.put("3","value222"); table.put("3","value333"); for (final Integer columnKey : table.columnKeyList()) { System.out.println(table.column(columnKey).values()); } // prints: // [value1,value111] // [value2,value22,value222] // [value3,value33,value333]
I deliberately use string for row keys [1,3,...] which are actually integers (as you did in the problem) and integers for column keys starting with 0 ([0,...]). Use get (int) of list to show the similarity with the previous example on the set of multimaps values
I hope this will help, mainly to determine what you want;)
Note: I use arraytable here because it has a more concise method to create a fixed set of row / key values (universe), rather than immutable table, but if you don't need variability, you should use it to change one - immutable table (and any other table implementation) doesn't have columnkeylist () method, but only columnkeyset () performs the same operation, But for arraytable, the speed is slow Of course, you should use immutable Builder or immutable table Copyof (table)