Does guava have a method for turning an iterable into a map of unique types?

As Jon Skeet mentioned, Maps.uniqueIndex is currently the closest thing to what you are looking for.

There are also a few requests for what you are looking for in the issue tracker, which you might want to "star" if you are interested in the suggested function:

http://code.google.com/p/guava-libraries/issues/detail?id=56

http://code.google.com/p/guava-libraries/issues/detail?id=460

http://code.google.com/p/guava-libraries/issues/detail?id=679

http://code.google.com/p/guava-libraries/issues/detail?id=718


The closest I'm aware of is Maps.uniqueIndex - that does the key side, but not the value side... is that close enough?

You could potentially use:

Map<K, V> map = Maps.transformValues(Maps.uniqueIndex(source, kProducer),
                                     vProducer);

Slightly awkward, but it would get the job done, I think...

Tags:

Java

Guava