30

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
Amit Kumar Gupta
  • 7,193
  • 12
  • 64
  • 90

4 Answers4

57

Google Collections (now Guava) is a Java 1.5 library... even ignoring the lack of generics in Java 1.4, it likely uses things that were added in 1.5, making it incompatible. That said, there are various ways to iterate through a Multimap.

By key, collection pairs in Java8:

multimap.asMap().forEach((key, collection) -> {...});

Iterate through all values:

for (Object value : multimap.values()) { ... }

Iterate through all keys (a key that maps to multiple values coming up multiple times in the iteration):

for (Object key : multimap.keys()) { ... }

Iterate through the key set:

for (Object key : multimap.keySet()) { ... }

Iterate through the entries:

for (Map.Entry entry : multimap.entries()) { ... }

Iterate through the value Collections:

for (Collection collection : multimap.asMap().values()) { ... }

You can also get the corresponding Collection for each key in the keySet() using get as described by bwawok.

Edit: I didn't think about the fact that Java 1.4 didn't have the foreach loop either, so of course each loop above would have to be written using the Iterators directly.

tkruse
  • 10,222
  • 7
  • 53
  • 80
ColinD
  • 108,630
  • 30
  • 201
  • 202
  • can't believe I'm so stupid that I was creating things like `HashMap>` for years instead of using a multi-map – ycomp Jun 17 '16 at 21:54
11

I am on Java 6, but this should be pretty close... sorry if I missed something java 1.4ish

    Set keySet = listmultimap.keySet();
    Iterator keyIterator = keySet.iterator();
    while (keyIterator.hasNext() ) {
        String key = (String) keyIterator.next();
        List values = listmultimap.get( key );

    }

Each get will get you everything back that matched that key. Then you can either peel those off, or do whatever you want with them.

bwawok
  • 14,898
  • 7
  • 32
  • 43
  • There is a single problem with my code. My HashMultimap may have HashMultimap as a value. and get() returns Set. I am not able to cast it again to HashMultimap for recursion. – Amit Kumar Gupta Oct 15 '10 at 04:15
  • Can you describe the use case of a multimap of multimaps :) Seems like maybe you could rethink it to be a more sane data structure, no? – bwawok Oct 15 '10 at 04:36
  • I am converting a XML file(small in size, so no prob) into a multimap. Since a tag can have tags inside. So a multimap can have multimap inside. – Amit Kumar Gupta Oct 15 '10 at 07:21
  • 1
    There are quite a few "fast" java XML parsers. Have you tried any of them? I guess it depends on what questions you want to ask the XML, but for just looping through and doing stuff with it, the normal XML parsing libraries seem easier.... – bwawok Oct 15 '10 at 12:19
  • Use a HashMultimap only when the values are immutable (at least to the extent that the hash code can't change). If the values are multimaps, define an ArrayListMultimap instead. On that multimap, get() returns a List. – Jared Levy Oct 16 '10 at 00:22
  • @bwawok: i dint found any parser which can do this sort of conversion. If someone is in ur mind tell me. – Amit Kumar Gupta Oct 19 '10 at 09:54
  • I recommend using regex to parse XML into a nested multimap structure. ;) – Nick Heiner Oct 25 '10 at 17:07
6

When you use guava multimap, the values are stored in Collection, not List

 Set<String> keys = multiMap.keySet();
    for (String keyprint : keys) {
        System.out.println("Key = " + keyprint);
        Collection<String> values = multiMap.get(keyprint);
        for(String value : values){
            System.out.println("Value= "+ value);
        }
    }
Jana
  • 189
  • 1
  • 3
  • 13
-1

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]
Kolibril
  • 1,096
  • 15
  • 19