I\'m reading the book \"Java 8 Lambdas\", and at some point the author says \"It’s a good idea to use the primitive specialized functions wherever possible because of the perfor
There's an extra level of boxing going on in
int sum1 = list.stream().reduce(0, (acc, e) -> acc + e).intValue();
as the reduction function is a BinaryOperator
- it gets passed two Integer
values, unboxes them, adds them, and then re-boxes the result. The mapToInt
version unboxes the Integer
elements from the list once and then works with primitive int
values from that point on as an IntStream
.