Collectors.groupingBy(classifier, downstream) signatue is not correct

Remi Forax forax at univ-mlv.fr
Sat Dec 7 08:57:20 PST 2013


This is something, I have discovered with Paul during Devoxx,
that I had forgotten until one of my student stubble on the very same 
issue Wednesday,
it's not a big issue, it's just something annoying.

Collectors.groupingBy that takes another collector as downstream 
collector is typed like this:
public static <T, K, A, D>
     Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends 
K> classifier,
                                           Collector<? super T, A, D> 
downstream) {
         ...
     }

here declaring A is useless because it is used only to type downstream.

This is annoying because another rule of Java,
when you specify implicitly a type argument for a type variable,
you cannot use '?' even if the type variable appears in place where '?' 
is allowed.
So the compiler is able to capture a wildcard to a type varaible but
a user can not do the same thing explicitly.

My student was trying to understand why the compiler was not compiling 
its code (hint: he fails to check the spelling first) and to isolate the 
issue, he was trying to explicitly specify the type argument.

I think the signature of groupBy (the one that takes a downstream)
should be
   public static <T, K, D>
     Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends 
K> classifier,
                                           Collector<? super T, ?, D> 
downstream) {
         ...
     }

(the declaration of A is removed and A is replaced by '?')

cheers,
Rémi



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