Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all Values from a Map for some Keys in Java/Guava?

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?

like image 593
FlorianOver Avatar asked Apr 19 '11 16:04

FlorianOver


People also ask

How do I get the value of a map for a specific key?

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.

How do you get all keys of a map as a list in Java?

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 .

How do you check all values in a HashMap?

To retrieve the set of keys from HashMap, use the keyset() method. However, for set of values, use the values() method.


4 Answers

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.

like image 57
ColinD Avatar answered Oct 19 '22 22:10

ColinD


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());
}
like image 20
Sean Patrick Floyd Avatar answered Oct 19 '22 23:10

Sean Patrick Floyd


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.

like image 21
skaffman Avatar answered Oct 19 '22 22:10

skaffman


Using guava: Collections2.transform(keys, Functions.forMap(map));

like image 25
Michael Brewer-Davis Avatar answered Oct 19 '22 22:10

Michael Brewer-Davis