TimurMahammadov / google-collections

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

Maps.aggregateIndex? #59

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I would find it useful to have a method that created an aggregate index, as
opposed to a unique one. Specifically, I might have a list of items that
can be mapped to keys, but there may be more than one value per key. So the
signature might look something like:

static
<K,V> Map<K,List<V>> 
aggregateIndex(Iterable<? extends V>, Function<? super V,? extends K>)

The implementation might look like:

{
    // TODO: Any desired null-checking
    final Map<K,List<V>> result = new HashMap<K,List<V>>();

    for(V value : values)
    {
        final K key = keyFunction.apply(value);
        List<V> valuesForKey = result.get(key);
        if(valuesForKey == null)
        {
            valuesForKey = new ArrayList<V>();
            result.put(key, valuesForKey);
        }
        valuesForKey.add(value);
    }
    return Collections.unmodifiableMap(result);
}

Original issue reported on code.google.com by estebis...@gmail.com on 10 Apr 2008 at 7:42

GoogleCodeExporter commented 9 years ago
Good news, something like this is in the works.  It returns a Multimap.  I 
believe we
simply called it "index()".

Original comment by kevin...@gmail.com on 10 Apr 2008 at 8:21

GoogleCodeExporter commented 9 years ago
Yeah, that would do it. Thanks.

Original comment by estebis...@gmail.com on 10 Apr 2008 at 8:49

GoogleCodeExporter commented 9 years ago
The internal version of the library now has Multimaps.index() methods. We'll 
include
them in the next code.google.com release.

Original comment by jared.l....@gmail.com on 3 May 2008 at 1:12