Is there a smart way to get all Values from a Map given some Keys?
I would like a method like this:
public static <K, V> Collection<V> getAll(Map<K, V> map, Collection<K> keys)
or is already a guava way?
Is there a smart way to get all Values from a Map given some Keys?
I would like a method like this:
public static <K, V> Collection<V> getAll(Map<K, V> map, Collection<K> keys)
or is already a guava way?
I agree with skaffman's answer, just not with his conclusion (I think this is better than manual iteration).
Here it is spelled out:
Also, here's a non-Guava version:
Java8 Streams:
Or if you're concerned about missing keys:
Or if your map is thread-safe and possibility large:
You could, I suppose use Guava's
Maps.filteredKeys()
, passing in aPredicate
which matches your desired keys, but it's not really any better than manual iteration.Using guava:
Collections2.transform(keys, Functions.forMap(map));
This depends on how you want the method to work. For example, should elements in
keys
that aren't inmap
A) just be ignored or should they B) be represented asnull
in the returned values collection or should that C) be an error? Also consider whether you want a live view or a separate collection containing the values.For A, my preference would be:
This limits the result to values for keys that are actually in the map and should be relatively efficient as well, even if the map is much larger than the set of keys you want. Of course, you may want to copy that result in to another collection depending on what you want to do with it.
For B, you'd use @Michael Brewer-Davis's solution except with
Functions.forMap(map, null)
.For C, you'd first want to check that
map.keySet().containsAll(keys)
and throw an error iffalse
, then use @Michael Brewer-Davis's solution... but be aware that unless you then copied the result in to another collection, removing an entry frommap
could cause anIllegalArgumentException
for code using the returned collection at some point.