I could not find a method in guava that converts a Collection (or Iterator/Iterable) to a Map, something like the following (wildcards omitted for clarity):
public static <T, K, V> Map<K,V> collectionSplitter(Collection<T> source, Function<T,K> kProducer, Function<T,V> vProducer){
Map<K,V> map = Maps.newHashMap();
for(T t : source){
map.put(kProducer.apply(t), vProducer.apply(t));
}
return map;
}
Is any existing method that does this? The closest I could find is Splitter.keyValueSplitter(), if T is a String.
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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With