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"?
Asked
Active
Viewed 4,066 times
2 Answers
14
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());

Cyrille Ka
- 15,328
- 5
- 38
- 58
-
I knew there had to be an easy way to do this! Thanks! :) – Jason Thompson Feb 22 '13 at 19:51
-
4If you've got an `ImmutableMultimap`, it has a built-in `inverse()` method. – Louis Wasserman Feb 22 '13 at 20:14
7
If you have an ImmutableMultimap
, which is a good idea whenever possible, you can call .inverse().get(v)
on it.

Kevin Bourrillion
- 40,336
- 12
- 74
- 87