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

Consumer Function

 
Ranch Hand
Posts: 47
  • 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!!
 
Bartender
Posts: 4912
185
  • 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: 1737
63
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.
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic