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?
HashMap get() Method in Java get() method of HashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.
Use keySet() to Get a Set of Keys From a HashMap in Java The simplest way to get the keys from a HashMap in Java is to invoke the keySet() method on your HashMap object. It returns a set containing all the keys from the HashMap .
To retrieve the set of keys from HashMap, use the keyset() method. However, for set of values, use the values() method.
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 (Java 8 or higher):
public static <K, V> Collection<V> getAll(Map<K, V> map, Set<K> keys) {
return map.entrySet()
.stream()
.filter(e -> keys.contains(e.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With