从映射中获取 Java/Guava 中某些键的所有值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5719833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:28:28  来源:igfitidea点击:

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

javacollectionsmapsguava

提问by FlorianOver

Is there a smart way to get all Values from a Map given some Keys?

有没有一种聪明的方法可以从给定一些键的 Map 中获取所有值?

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?

或者已经是番石榴方式?

回答by ColinD

This depends on how you want the method to work. For example, should elements in keysthat aren't in mapA)just be ignored or should they B)be represented as nullin 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.

这取决于您希望该方法如何工作。例如,keys不在mapA) 中的元素应该被忽略还是应该B)null在返回值集合中表示,或者C)应该是错误?还要考虑您是想要实时视图还是包含值的单独集合。

For A, my preference would be:

对于A,我的偏好是:

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).

对于B,您将使用@Michael Brewer-Davis 的解决方案,除了 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 mapcould cause an IllegalArgumentExceptionfor code using the returned collection at some point.

对于C,您首先要检查map.keySet().containsAll(keys)并抛出错误 if false,然后使用@Michael Brewer-Davis 的解决方案...但请注意,除非您随后将结果复制到另一个集合中,否则从中删除条目map可能会导致IllegalArgumentException对于在某个时候使用返回的集合的代码。

回答by Sean Patrick Floyd

I agree with skaffman's answer, just not with his conclusion (I think this is better than manual iteration).

我同意 skaffman 的回答,只是不同意他的结论(我认为这比手动迭代要好)。

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();
}

回答by skaffman

You could, I suppose use Guava's Maps.filteredKeys(), passing in a Predicatewhich matches your desired keys, but it's not really any better than manual iteration.

我想您可以使用 Guava 的Maps.filteredKeys(),传入Predicate与您想要的键匹配的a ,但这并不比手动迭代更好。

回答by Michael Brewer-Davis

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

使用番石榴: Collections2.transform(keys, Functions.forMap(map));

回答by Code Eyez

Java8 Streams:

Java8 流:

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() );