Lambda Expression in Java 8

Lambda expression was introduced in Java 8.

Java lambda expressions are Java’s first step into functional programming.

A lambda expression is an anonymous function. A function that doesn’t have a name and doesn’t belong to any class.

Syntax of Lambda Expression

(parameters) -> {expression or statements}

Examples:

Zero parameter:
() -> System.out.println(“no parameters”);

One parameter:
(param) -> System.out.println(“Single param: ” + param);

Multiple parameters :
(param1, param2) -> System.out.println(“Multiple params: ” + param1 + “, ” + param2);

Functional interfaces provide target types for lambda expressions and method references. Please visit Functional interfaces for more stuff.

Let’s have a look into the below codes for better understanding of Lambda Expressions.

E8

Output

O8

Using Lambda for looping through list

E9

Output

O9

Using Lambda for Running a Thread

E10

Output

O10

Leave a Reply

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