This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Question regarding 2 different ways of using lambda functions in Java

 
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen 2 different ways of using Lambda functions in Java

1)  Assign the function to an object of type functional interface,  call the method of functional interface on this object while passing argument to this interface.

2) Pass the function itself as an argument to some method.

I have a question regarding these 2 ways :

When do we have to use 1 instead of 2 and when do we have to use 2 instead of 1.

Thanks

 
Saloon Keeper
Posts: 10669
228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference? A method parameter that accepts a function is itself a variable of a functional interface type.
 
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . When do we have to use 1 instead of 2 and when do we have to use 2 instead of 1. . . .

When would you writeand whenYou should get the same answer.
 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Often option 1 can be considered as an "intermediate" step, which could be skipped by passing the lambda as such (i.e. step 2).
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it called intermediate when it is doing the complete thing.?
 
Greenhorn
Posts: 5
VI Editor Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, from your description I came up with something like this:
Although, I don't understand "passing argument to this interface", do you mean passing argument to method of used functional interface / object of given functional interface ?

1)

2)

having defined:



Because, if this is the case, then those are 2 different cases. In first one, you are actually also calling for execution and providing actual argument of functional interface function, whereas in second example you only define behavior, but have not power over actual argument or execution.

If you meant by that description scenario like this:

1)

2)

having defined:



Then I guess it depends on reusability of that function. Same as some Strings in class, if you use them on multiple places, define it as field/constant in class otherwise second approach is more suitable.
 
I think I'll just lie down here for a second. And ponder this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!