Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:17:00+00:00 2026-05-31T15:17:00+00:00

Using the new collections from Google’s Guava, http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained How do I loop over a

  • 0

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";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:17:01+00:00Added an answer on May 31, 2026 at 3:17 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the GSON library and calling the fromJson() method: http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/Gson.html My old code:
Consider this simplest piece of code: using System; using System.Collections.Generic; using System.Linq; using System.Text;
I am using the Google Collections library AbstractIterator to implement a generator. I ran
When I execute the following code, I get ConcurrentModificationException Collection<String> myCollection = Collections.synchronizedList(new ArrayList<String>(10));
I am upgrading from Google Collections 0.9 to 1.0. It seems Sets.newConcurrentHashSet() is no
when using new Date,I get something like follows: Fri May 29 2009 22:39:02 GMT+0800
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
When you allocate an array using new [] , why can't you find out
ADO.NET has the notorious DataRow class which you cannot instantiate using new. This is
What is the time complexity of dynamic memory allocation using new, malloc, etc.? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.