• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

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

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!
Helene
 
author & internet detective
Posts: 41860
908
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!
reply
    Bookmark Topic Watch Topic
  • New Topic