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:
Slightly awkward, but it would get the job done, I think…