How to iterate through google multimap
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.
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.