cannot find symbol on identity operation in lambda
Brian Goetz
brian.goetz at oracle.com
Tue Nov 27 21:03:18 PST 2012
The problem is that the type of the stream returned by map() is
Stream<Integer>, not IntStream.
If you wanted an IntStream, you'd want to explicitly convert to ints:
...stream().map(i -> (int) i).sum()
Given the overloaded map methods:
map(T -> T)
and
map(T -> int)
the former is a more applicable match for i->i than the latter, since it
does not require unboxing.
On 11/27/2012 11:29 PM, Arul Dhesiaseelan wrote:
> Arrays.asList(1, 2, 3, 4, 5).stream().map(i -> i).sum();//compile error
>
> java: cannot find symbol
> symbol: method sum()
> location: interface java.util.stream.Stream<java.lang.Integer>
>
>
> Arrays.asList(1, 2, 3, 4, 5).stream().map((IntFunction<Integer>)i ->
> i).sum();//works fine with a cast on identity
> Arrays.asList(1, 2, 3, 4, 5).stream().map(i -> i * 2).sum();//works fine
> without a cast, but not identity
>
> Is this expected behavior on an identity operation in a lambda and requires
> explicit typing?
>
> -Arul
>
More information about the lambda-dev
mailing list