Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Stream reduce method, must the identity always be 0 for sum and 1 for multiplication?

Tags:

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?

P.S.

for me, 73 is expected result but 76 is not.

like image 580
gstackoverflow Avatar asked Sep 30 '15 12:09

gstackoverflow


People also ask

What is identity in stream reduce ()?

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 .

What happens if a reduction operation has no identity element?

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.

How does reduce work in Java stream?

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.

How should we in a stream to calculate sum of elements?

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.


1 Answers

The identity value is a value, such that x op identity = x. This is a concept which is not unique to Java Streams, 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) 
like image 102
Holger Avatar answered Nov 12 '22 05:11

Holger