Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

guava

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.

like image 832
TDJoe Avatar asked Oct 13 '11 17:10

TDJoe


2 Answers

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...

like image 143
Jon Skeet Avatar answered Oct 15 '22 14:10

Jon Skeet


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

like image 39
Etienne Neveu Avatar answered Oct 15 '22 16:10

Etienne Neveu