inconsistency in invoking method

Brian Goetz brian.goetz at oracle.com
Sun Mar 3 15:14:25 PST 2013


This is correct.  What's happening is that in the first example, the 
most natural overload of map() is map(ToDoubleFunction) because the 
shape of the lambda provided most naturally matches Integer -> double, 
so it selects the overload that returns a DoubleStream, which has a 
sum() method.  In the second, it selects the map(Function), returning a 
Stream<Integer>, which has no sum() method.

If you do:

   Arrays.asList(1.0).stream().map(n -> (int) n).sum()
or
   Arrays.asList(1.0).stream().map(n -> Integer.intValue(n)).sum()
or
   Arrays.asList(1.0).stream().map(Integer::intValue).sum()

you'll get what you want.

(Keep repeating: we love erasure, we love erasure...)


On 3/3/2013 5:53 PM, Venkat Subramaniam wrote:
> Greetings,
>
> Not sure if this is by intent, but the following inconsistency is confusing and would be great
> if can be resolved:
>
>      System.out.println(
>        Arrays.asList(1.0).stream().map(n -> n * 1.0).sum()
>      ); //Works fine, prints 1.0
>
> However,
>
>      System.out.println(
>        Arrays.asList(1.0).stream().map(n -> n).sum()
>      ); //error: cannot find symbol
>
> Thanks
>
> Venkat
>


More information about the lambda-dev mailing list