1

I have a

Multimap<String, Integer> map = ...

where I can do map.get("somekey") to retrieve all the matching Integers.

Now I would like to find the keys that have a given Integer, i.e. 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.

Is there an easy way to do this in Google Guava?

centic
  • 15,565
  • 9
  • 68
  • 125

1 Answers1

1

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.

centic
  • 15,565
  • 9
  • 68
  • 125
  • 1
    [See my answer to similar question](http://stackoverflow.com/questions/8066109/bidirectional-multi-valued-map-in-java/8439744#8439744). "Faster" alternative to your solution can be [`ImmutableMultimap.inverse()`](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ImmutableMultimap.html#inverse()). What you really want here *if you want mutability* is `BiMultimap`, which Guava does not have, see this [closed issue](https://github.com/google/guava/issues/394). If you have valid use case for *mutable* `BiMultimap`, please provide your case in the issue's comment. – Grzegorz Rożniecki Jan 29 '15 at 12:53