Is there a way to get all keys from a value in a multimap?

独自空忆成欢 提交于 2019-12-08 17:08:10

问题


Say I have a guava Multimap. I have a value, "Foo", that may belong to one or more keys. Is there any way I can figure out which keys contain an entry "Foo"?


回答1:


You can invert the Multimap. For this you can use the method Multimaps.invertFrom.

For example, if your Multimap is a Multimap<String, String>

Multimap<String, String> invertedMultimap = Multimaps.invertFrom(myMultimap, ArrayListMultimap.<String, String>create());



回答2:


If you have an ImmutableMultimap, which is a good idea whenever possible, you can call .inverse().get(v) on it.



来源:https://stackoverflow.com/questions/15031903/is-there-a-way-to-get-all-keys-from-a-value-in-a-multimap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!