aspose file tools*
The moose likes Features new in Java 8 and the fly likes how to use lambdas in java8 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "how to use lambdas in java8" Watch "how to use lambdas in java8" New topic
Author

how to use lambdas in java8

Karina Guenther
Ranch Hand

Joined: Sep 16, 2005
Posts: 55
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

Joined: May 26, 2003
Posts: 31057
    
232

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Richard Reese
author
Ranch Hand

Joined: Jul 13, 2011
Posts: 84
    
    5

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.


Richard Reese
Java 8 New Features: A Practical Heads-Up Guide
Karina Guenther
Ranch Hand

Joined: Sep 16, 2005
Posts: 55
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

Joined: Sep 16, 2005
Posts: 55
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

Joined: May 26, 2003
Posts: 31057
    
232

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use lambdas in java8