Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"?

like image 967
Jason Thompson Avatar asked Feb 22 '13 19:02

Jason Thompson


2 Answers

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

like image 91
Kevin Bourrillion Avatar answered Oct 04 '22 00:10

Kevin Bourrillion


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());
like image 41
Cyrille Ka Avatar answered Oct 03 '22 22:10

Cyrille Ka