简体   繁体   English

交换Google Guava Multimap中的键和值的关系

[英]Swapping relation of keys and values in a Google Guava Multimap

I have a 我有一个

Multimap<String, Integer> map = ...

where I can do map.get("somekey") to retrieve all the matching Integers. 我可以在其中执行map.get(“ somekey”)来检索所有匹配的Integer。

Now I would like to find the keys that have a given Integer, ie something like a 现在,我想找到具有给定整数的键,例如

Collection<String> keys = map.getByValue(Integer.of(4))

which returns all keys in the Multimap where the given Integer is stored as value. 它返回Multimap中所有给定Integer作为值存储的键。

Is there an easy way to do this in Google Guava? Google Guava中有一种简单的方法吗?

Shortly after posting the question I found the following which does the Job nicely: 发布问题后不久,我发现以下内容可以很好地完成工作:

Multimap<String, Integer> reversed = ...
Multimaps.invertFrom(map, reversed);

It will actually do a copy, a solution that does this without actually copying all the entries would still be interesting. 它实际上会进行复制,而无需实际复制所有条目的解决方案仍然很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM