Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

passing argument to nested biconsumer

 
Ranch Hand
Posts: 590
6
jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am going through a tutorial for lambda functions and came across an example using biconsumer of biconsumer.

Here is a sample code from that lesson:



The lesson did not explain one thing. If I create a biconsumer of biconsumer, how do I pass the arguments to it.

Would you please provide some insight into this scenario.

Thanks

edit: corrected the title to reflect the proper question.
 
Saloon Keeper
Posts: 12154
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you see a BiConsumer of BiConsumer?

If you are referring to the empBonusAndHikeConsumer field, it's not a BiConsumer of BiConsumer. It's a BiConsumer of an employee and a percentage, just as the other two consumers are.

As the name implies, it accepts two arguments, not three. The bonus parameter in your testBiConsumer() method is used as the argument for both the bonus and the hike parameters of the lambda expressions.
 
s ravi chandran
Ranch Hand
Posts: 590
6
jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I understood it to be biconsumer of biconsumer.

for the field empBonusAndHikeConsumer, how can I pass bonus of 5% and hike of 15%. That is the expected behavior.
 
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you study the source code for the BiConsumer.andThen() method, you'll see what Stephan was saying earlier. Bottom line: a composed BiConsumer will use the same arguments for each BiConsumer in the sequence.

http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/function/BiConsumer.java

What this means to you is that you can't do what you want to do if you want to use these in the spirit they're meant to be used. However, if you really want to do it regardless of how ugly the code gets, you can move the lambda creation into the method and capture the various values from the parameters. This means that you'll essentially ignore the second parameter passed to the lambda. But like I said, it would be ugly and kind of pointless.
 
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such material comes from. We can assess the tutorial; most of the tutorials I see on the Net are rather bad.
You are getting confused about the andThen method. Please start by looking at that link. It produces a bi consumer doing two things.
 
s ravi chandran
Ranch Hand
Posts: 590
6
jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses. Yes, I see that the requirement for this example cannot be implemented with nested biconsumers. well I did understand the concept of biconsumer, only this example was confusing to me.

Meanwhile this lesson is from tutorial link and this particular lesson is from chapter "Additional Functions" section "Two Argument BiConsumers".

Edit: checked the lesson again, the presenter created empBonusAndHikeConsumer but did not use it anywhere. So in effect I was wrong to call it from testBiConsumer. Strangely though if this field is not used anywhere just defining the nested biconsumer does not seem that useful.
 
Stephan van Hulst
Saloon Keeper
Posts: 12154
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

s ravi chandran wrote:Yes, I see that the requirement for this example cannot be implemented with nested biconsumers.


Be precise when using programming terms. They are not nested. They are chained or composed.
 
Campbell Ritchie
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

s ravi chandran wrote:. . . this lesson is from tutorial link

Thank you for the info Unfortunately, I don't pay for an O'Reilly account, so I shan't open that link properly.

. . . nested biconsumer . . .

We have already decided it isn't anything nested.
 
s ravi chandran
Ranch Hand
Posts: 590
6
jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my bad  will correct the title again..

My sample is mostly different from the original example in two ways, one is use of Emp class instead of Employee class which I did not find anywhere and other is use of empBonusConsumer in method testBiConsumer(). So biconsumer empBonusAndHikeConsumer is created and not used anywhere.

Here is the code snippet from lesson:


But the confusion I had with this example is resolved.
 
Campbell Ritchie
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

s ravi chandran wrote:. . . will correct the title again.. . . .

Please don't; it makes it looks as if we were answering a non‑question.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic