Java – Filtering Guava Multimaps

guavajava

Is there a a built-in method or combination of methods to return a filtered view of Guava ImmutableMultimaps using predicates, like you can with regular maps?

There does not appear to be Maps.filter method that accepts an ImmutableMultimap as a parameter. Looking at the API I could call asMap() and get a map based view of the multimap and perform the filter that way. However, I need to return an ImmutableMultimap from my function and for obvious reasons there isn't a way to view a Map> as an ImmutableMultimap – without having to contruct a new Multimap.

Even if I could filter it as a Map and cast it back to an ImmutableMultimap since they are all just views (I think?), the filter methods would only let me filter on the collection as a whole not removing individual values.

Best Answer

Multimaps.filterEntries was added to Guava in release 11.

Related Topic