I have to iterate through google multimap. But
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 Collection
s:
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 Iterator
s directly.