• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Consumer Function

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dears,

I was practcing for the OCP test and came across the below code. As per my understanding the answer that i assumed was 11 but the actual answer is 10. Can someone please help me undrerstand this piece of code.

Thanks in advance!!
 
Saloon Keeper
Posts: 4612
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ask yourself what is being printed in the print-consumer: Counter.count or the argument 10?
 
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is interesting!

.andThen() has a similar syntax when used on a Consumer and a Function, but very different semantics1

.andThen() and .compose() both involve chaining a result in different ways when it comes to Function/UnaryOperator.

There is still an .andThen() for Consumer, but no result chaining, because there is no Result, only a side effect.

Nice subtle question, I think, that goes to the understanding of these two different functional interfaces.

This understanding gets further muddled, if one is not careful, with the .collect() method.  Because that takes parameters of BiConsumer<> type, that very often DO return a result that could be used for chaining, but is not by .collect() but "appears to be" because they work on mutable objects.

I was actually thinking about this state of affairs yesterday and this morning.

Subtleties abound in Lambdas and Streams usage.
 
reply
    Bookmark Topic Watch Topic
  • New Topic