• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Requirements needed to properly use reduce() on parallel streams (Boyarsky,selikoff)

Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

On page 374 of the OCP 8 Programmer II, the authors list the requirements needed to use reduce() on parallel streams. However, I don't understand a thing about the explanations given after each bullet point. I know how the reduce() method works, I just don't understand the rules.

■ The identity must be defined such that for all elements in the stream p ,
combiner.apply(identity, p) is equal to p .

■ The accumulator operator op must be associative and stateless such that (a op b) op c
is equal to a op (b op c) .

■ The combiner operator must also be associative and stateless and compatible with the
identity, such that for all p and t combiner

Thank you in advance for the help!
author & internet detective
Posts: 40685
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some of these rules matter more for parallel streams.

This code break the rule about identity and outputs something like -w-o-l-f. Why? Because the identity isn't really an identity. You should be able to apply an identity as many times as you want without the answer changing. That's why empty string is a far better identity value!

Now let's break the other rules. What's the output of this

The correct answer is that we don't know. IT depends on the order the stream elements are processed. Not good!
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic