Java 8: Lambda walkthrough

Java 8

Previously you can find article about what’s new in Java 8, in this tutorial let me show you some examples in using Lambda expression in Java 8.┬áLambda expressions are used to assign the behavior to a variable or pass the behavior in between method calls instead of wrapping the behavior in an object and working with the objects.

Take a look at the following examples to understand the Lambda expressions better.

Runnable (very simple case)

Note the syntax, informally as:

The arrow is a new operator. And note the conciseness of the second piece of code compared to the more bulky first piece.

Collections

Let say we have few classes below (Human and HumanHelper):

 

Then we create main class (old way) to use Human and HumanHelper;

Let’s compare with main class with Lambda below:

Both codes above will show same output as you can see below:

and,

 

Another example about Lambda (Map, Reduce, and Optional)

And the output for above example is here:

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*