Tag Archives: jdk


The new lambda expressions included in Java Platform Standard Edition 8 (Java SE 8).

Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection. In addition, new concurrency features improve performance in multicore environments.

The following is a list of hardware and software requirements:

  • Java Development Kit (JDK 8) early access
  • NetBeans 7.4

Lambda Expression Syntax

Lambda expressions address the bulkiness of anonymous inner classes by converting five lines of code into a single statement. This simple horizontal solution solves the "vertical problem" presented by inner classes.

A lambda expression is composed of three parts.

Argument List Arrow Token Body
(int x, int y) -> x + y

The body can be either a single expression or a statement block. In the expression form, the body is simply evaluated and returned. In the block form, the body is evaluated like a method body and a return statement returns control to the caller of the anonymous method. The break and continue keywords are illegal at the top level, but are permitted within loops. If the body produces a result, every control path must return something or throw an exception.


Here is how you write a Runnable using lambdas.

 6 public class RunnableTest {
 7   public static void main(String[] args) {
 9     System.out.println("=== RunnableTest ===");
11     // Anonymous Runnable
12     Runnable r1 = new Runnable(){
14       @Override
15       public void run(){
16         System.out.println("Hello world one!");
17       }
18     };
20     // Lambda Runnable
21     Runnable r2 = () -> System.out.println("Hello world two!");
23     // Run em!
24     r1.run();
25     r2.run();
27   }
28 }

In both cases, notice that no parameter is passed and is returned. The Runnable lambda expression, which uses the block format, converts five lines of code into one statement.

Reference Source for this information is www.oracle.com