rajrembo / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Create MultiMaps.invertFrom(Map) method #201

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I have situations where a number of keys may point to the same value in a 
map, and would like to be able to invert the Map to get the list of keys for 
each value.

At the moment, this requires about 4 calls to MultiMaps to do all the 
conversions, and get the Supplier.  

It's also a bit lengthy to have to pass Suppliers around, but I guess that's 
deliberate. In the case of a Map, the key collection would necessarily be a 
Set.

Original issue reported on code.google.com by step...@kestle.homeip.net on 24 Jun 2009 at 11:32

GoogleCodeExporter commented 8 years ago
You can do this using:

Multimaps.invertFrom(Multimaps.forMap(map))

or

Multimaps.index(map.keySet(), Functions.forMap(map))

The first is probably better.

By the way, it sounds like you might be using Multimaps.newMultimap(Map, 
Supplier) in
a very different way from how it's intended. The supplier is only supposed to 
supply
empty collections. But I'm confused and maybe misunderstood.

Original comment by kevin...@gmail.com on 25 Jun 2009 at 12:35