Is there a way to get all the keys for a value in a multimar?

Say I have a guava moussam. I have a value of "Foo" that can belong to one or more keys. Is there any way to find out which keys contain the "Foo" record?

+8
java multimap guava
source share
2 answers

You can invert multimap. You can use the Multimaps.invertFrom method for Multimaps.invertFrom .

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

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

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

+6
source share

All Articles