Additional Collectors

Paul Sandoz paul.sandoz at oracle.com
Thu Apr 4 05:02:36 PDT 2013


On Apr 3, 2013, at 8:34 PM, Tim Peierls <tim at peierls.net> wrote:

> On Wed, Apr 3, 2013 at 2:02 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> 
>> I think if we do
>>  Map<T,U> toMap(T->U)
>> and
>>  Map<K,U> toMap(T->K, T->U)
>> 
>> we can call them both toMap and people will get it.
> 
> 
> I think that's fine, but if you do that it would be really nice to show how
> to get the "toIndexMap" behavior in the docs for the two-arg toMap.
> 

I quite like the fact, at the moment, that toMap always uses an element as the key and maps element to values. It is an easy rule to remember.

Where as groupingBy always requires a classifiying function to map an element to a key, plus many ways to collect elements associated the same key, the canonical one being to collect elements to a list [*].

There seems another basic use-case which is toUniqueIndexMap:

  Map<U, T> toUniqueIndexMap(T->U)

and then we don't need to merge values of T. If that is required then groupingBy could be used instead.

Perhaps documentation-wise it may be helpful to provide examples of how toMap etc could be implemented using groupingBy?

Paul.

[*] I  wondering whether we really need the explicit List returning variants of groupingBy.


More information about the lambda-libs-spec-experts mailing list