Search code examples
lambdaforeachjava-8java-streamunary-operator

why does forEach method on Collection and Stream take Function.Identity successfully even if it is not Consumer type?


IntStream is = IntStream.of(1,2,3);

IntUnaryOperator un = IntUnaryOperator.identity();

is.forEach(s -> un.applyAsInt(s));

forEach method can only take consumer as the argument then how does it compile successfully with applyAsInt man method which returns int?

Can anyone please explain the reason behind this?


Solution

  • Because "s -> un.applyAsInt(s)" is a Consumer