Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream's 3 argument reduce method #547

Open
kheekheekhee opened this issue Nov 29, 2020 · 2 comments
Open

Stream's 3 argument reduce method #547

kheekheekhee opened this issue Nov 29, 2020 · 2 comments
Labels
🖱️ Java General Questions with regards to Java or its API

Comments

@kheekheekhee
Copy link

Hi, can someone explain how Stream's 3 argument reduce method works with identity, accumulator and combiner?
And how to ensure that it gives the correct answer in the correct order when done in parallel and how to check when it doesn't give the correct answer?

@kheekheekhee kheekheekhee added the 🖱️ Java General Questions with regards to Java or its API label Nov 29, 2020
@rcdl2222
Copy link

I found this website that may be helpful:

https://ocpj8.javastudyguide.com/ch18.html

@Octanis0
Copy link

Octanis0 commented Nov 29, 2020

If you have a stream containing objects of type T, then the reduce method will take in :

  1. an identity of some type U,
  2. an accumulator (two inputs of type U and T, output of type U),
  3. and a combiner (two inputs both of type U, output of type U).

The sequence of events inside reduce roughly goes as follows:

  1. At the beginning of the reduce operation, you have one type U object (identity) and the rest are all of type T (from your stream).
  2. The reduce method starts off by using the accumulator on the identity and some of your stream elements to create intermediate U objects.
  3. As more type U intermediate objects are generated from the accumulator, the combiner gets to work combining them together.

The endgoal of reduce is to combine/accumulate everything until you have one final type U object. Regardless of whether the setting was sequential or parallel, the accumulator and combiner, when invoked, will take up whatever available objects that fit their respective input types and complete the operation. They are usually invoked in an arbitrary order, as long as there are enough available objects to use it on.

Another thing to note is that your combiner may combine an intermediate U object with the identity. It does not distinguish between the two.

This is why your accumulator and combiner have to be associative, so that your end result is still the same regardless of the sequence of accumulation/combination. You also have to make sure to choose an identity carefully so that an intermediate object that gets combined with the identity doesn't change.

How to ensure these conditions are met is quite context-specific... I'm not sure if there's really a generic way to check for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🖱️ Java General Questions with regards to Java or its API
Projects
None yet
Development

No branches or pull requests

3 participants