How to iterate through google multimap

Viewed 59066

I have to iterate through google multimap. But

  1. I am using jdk 1.4 and can't switch to higher version. So i can not use generic features.
  2. My multimap can have multiple values for a key.
  3. There might be a situation when a value of multimap is multimap in itself
4 Answers

A minimal example would be:

public class Test {
    public static void main(String[] args) {
        ListMultimap<String, String> multimap = ArrayListMultimap.create();

        multimap.put("hello", " name");
        multimap.put("hello", " name2");
        multimap.put("world", " ocean");

        for (String firstName : multimap.keySet()) {
            List<String> lastNames = multimap.get(firstName);
            System.out.println(firstName + " " + lastNames);
        }
    }
}

and the output:

world [ ocean]
hello [ name,  name2]
Related