guava: Best way to iterate over the key->collection entries of a Multimap?

Viewed 8631

I'm looking for the corresponding way, for Multimap, to iterate over entries of a Map, namely:

Map<K,V> map = ...;
for (Map.Entry<K,V> entry : map.entrySet())
{
    K k = entry.getKey();
    V v = entry.getValue();
}

Which of the following is better? (or perhaps more importantly, what are the differences?)

Multimap<K,V> mmap = ...;
for (Map.Entry<K,Collection<V>> entry : mmap.asMap().entrySet())
{
    K k = entry.getKey();
    Collection<V> v = entry.getValue();
}

or

Multimap<K,V> mmap = ...;
for (K k : mmap.keySet())
{
    Collection<V> v = mmap.get(k);
}
2 Answers
Related