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?
This depends on how you want the method to work. For example, should elements in keys
that aren't in map
A) just be ignored or should they B) be represented as null
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:
Collection<V> values = Collections2.transform(
Collections2.filter(keys, Predicates.in(map.keySet()),
Functions.forMap(map));
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 if false
, then use @Michael Brewer-Davis's solution... but be aware that unless you then copied the result in to another collection, removing an entry from map
could cause an IllegalArgumentException
for code using the returned collection at some point.
I agree with skaffman's answer, just not with his conclusion (I think this is better than manual iteration).
Here it is spelled out:
public static <K, V> Collection<V> getAll(Map<K, V> map, Collection<K> keys) {
return Maps.filterKeys(map, Predicates.in(keys)).values();
}
Also, here's a non-Guava version:
public static <K, V> Collection<V> getAll(Map<K, V> map, Collection<K> keys) {
Map<K, V> newMap = new HashMap<K, V>(map);
newMap.keySet().retainAll(keys);
return newMap.values();
}
You could, I suppose use Guava's Maps.filteredKeys()
, passing in a Predicate
which matches your desired keys, but it's not really any better than manual iteration.
Using guava:
Collections2.transform(keys, Functions.forMap(map));
Java8 Streams:
keys.stream().map( map::get ).collection( Collection.toList() );
Or if you're concerned about missing keys:
keys.stream().map( k -> map.getOrDefault( k, null ) ).collection( Collection.toList() );
Or if your map is thread-safe and possibility large:
keys.parallelStream().map( map::get ).collection( Collection.toList() );