~/ learn/ java-from-zero/ cards/ Terminal ops & Collectors
1 of 4

Type a reduce that sums a stream of integers

Type a reduce that sums a stream of integers

Answer

int sum = Stream.of(1, 2, 3, 4, 5) .reduce(0, Integer::sum);

reduce(identity, accumulator): starts with 0 and applies Integer::sum to fold each element in. Returns an int directly when an identity is provided.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/4be45e35-c110-4e2f-9205-5c7609b77ef3/flashcard utf-8 LF