I proceed with java 8 learning.
I have found an interesting behavior:
let's see code sample:
// identity value and accumulator and combiner Integer summaryAge = Person.getPersons().stream() //.parallel() //will return surprising result .reduce(1, (intermediateResult, p) -> intermediateResult + p.age, (ir1, ir2) -> ir1 + ir2); System.out.println(summaryAge);
and model class:
public class Person { String name; Integer age; ///... public static Collection<Person> getPersons() { List<Person> persons = new ArrayList<>(); persons.add(new Person("Vasya", 12)); persons.add(new Person("Petya", 32)); persons.add(new Person("Serj", 10)); persons.add(new Person("Onotole", 18)); return persons; } }
12+32+10+18 = 72
. For sequential stream, this code always returns 73
which is 72 + 1
but for parallel, it always returns 76
which is 72 + 4*1
(4 is equal to stream elements count).
When I saw this result I thought that it is strange that parallel stream and sequential streams return different results.
Am I broke contract somewhere?
for me, 73 is expected result but 76 is not.
The identity, which is the first element of reduce , must satisfy combiner(identity, u) == u . Quoting the Javadoc of Stream. reduce : The identity value must be an identity for the combiner function. This means that for all u , combiner(identity, u) is equal to u .
Identity is the default result of reduction if there are no elements in the stream. That's the reason, this version of reduce method doesn't return Optional because it would at least return the identity element. Ignoring this rule will result in unexpected outcomes.
Reducing is the repeated process of combining all elements. reduce operation applies a binary operator to each element in the stream where the first argument to the operator is the return value of the previous application and second argument is the current stream element.
sum() The Stream API provides us with the mapToInt() intermediate operation, which converts our stream to an IntStream object. This method takes a mapper as a parameter, which it uses to do the conversion, then we can call the sum() method to calculate the sum of the stream's elements.
The identity value is a value, such that x op identity = x
. This is a concept which is not unique to Java Stream
s, see for example on Wikipedia.
It lists some examples of identity elements, some of them can be directly expressed in Java code, e.g.
reduce("", String::concat)
reduce(true, (a,b) -> a&&b)
reduce(false, (a,b) -> a||b)
reduce(Collections.emptySet(), (a,b)->{ Set<X> s=new HashSet<>(a); s.addAll(b); return s; })
reduce(Double.POSITIVE_INFINITY, Math::min)
reduce(Double.NEGATIVE_INFINITY, Math::max)
It should be clear that the expression x + y == x
for arbitrary x
can only be fulfilled when y==0
, thus 0
is the identity element for the addition. Similarly, 1
is the identity element for the multiplication.
More complex examples are
Reducing a stream of predicates
reduce(x->true, Predicate::and) reduce(x->false, Predicate::or)
Reducing a stream of functions
reduce(Function.identity(), Function::andThen)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With