Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use lambdas in java8

 
Karina Guenther
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recently took a class on new features in Java 8 and the hardest thing for me to understand are lambdas and when I should use them vs. whatever I'm coding now.

for example take:


When I try to code something myself I couldn't quite get how x gets fed. And then I ran into the concept that I wanted to grab the value that my function had processed. I can't remember if your lambda function can only return void?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karina,
I find lambdas to be useful when I'm chaining operations. And no, lambdas aren't required to return void.

This shows two lambdas. The first returns and int. (x+3). The other just prints out a number.


Granted this is only two operations. But even here, it lets me focus on one thing at a time. Which means I can write the calculation logic separately. This also lets me pass in the lambda as a parameter and use the same stream/map/forEach statement with a System.out.println or writing to a file or doing something else.
 
Richard Reese
author
Ranch Hand
Posts: 84
5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karina,
Lambdas are quite flexible. They can be passed multiple parameters and return values. Arguments types can be implied in many situations and lambda bodies can be multiple lines. Your concern about “how c gets fed” is a good one. This is one of those neat things that the compiler does for you. It is able to infer the data type and then “get” the values based on the lambda expressions’ context. Lambda expressions do not contain information about which functional interface it is implementing. It all depends on the context in which it is being used.

 
Karina Guenther
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Karina,

This shows two lambdas. The first returns and int. (x+3). The other just prints out a number.

Interesting example. So, the values in myints go into x-> x+3 which are mapped to a stream and for each of the results print it out? Did I say that right? I only figured out what was happening to the results of x when I copied your code and ran it.

If instead you wanted to save the results for use at another time would it be something like this:


Basically, I think seeing lots of examples and how it's being used will be what I need to start thinking of how to use them in my own code. Not to mention all the other new features that go along with them.
 
Karina Guenther
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Reese wrote:Karina,
Lambdas are quite flexible. They can be passed multiple parameters and return values. Arguments types can be implied in many situations and lambda bodies can be multiple lines. Your concern about “how c gets fed” is a good one. This is one of those neat things that the compiler does for you. It is able to infer the data type and then “get” the values based on the lambda expressions’ context. Lambda expressions do not contain information about which functional interface it is implementing. It all depends on the context in which it is being used.



Yes, I can see where context is vital in determining how to use them. I can sense how powerful they'll be once I understand them as well as I do on how/when I use an List, Map or Set.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting example. So, the values in myints go into x-> x+3 which are mapped to a stream and for each of the results print it out? Did I say that right? I only figured out what was happening to the results of x when I copied your code and ran it.

Correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic