Javatpoint Logo
Javatpoint Logo

Java Lambda Expressions

Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection.

The Lambda expression is used to provide the implementation of an interface which has functional interface. It saves a lot of code. In case of lambda expression, we don't need to define the method again for providing the implementation. Here, we just write the implementation code.

Java lambda expression is treated as a function, so compiler does not create .class file.

Functional Interface

Lambda expression provides implementation of functional interface. An interface which has only one abstract method is called functional interface. Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface.


Why use Lambda Expression

  1. To provide the implementation of Functional interface.
  2. Less coding.

Java Lambda Expression Syntax

Java lambda expression is consisted of three components.

1) Argument-list: It can be empty or non-empty as well.

2) Arrow-token: It is used to link arguments-list and body of expression.

3) Body: It contains expressions and statements for lambda expression.

Let's see a scenario. If we don't implement Java lambda expression. Here, we are implementing an interface method without using lambda expression.


Without Lambda Expression

Test it Now

Output:

Drawing 10

Java Lambda Expression Example

Now, we are implementing the above example with the help of lambda expression.

Test it Now

Output:

Drawing 10

A lambda expression can have zero or any number of arguments. Let's see the examples:

Java Lambda Expression Example: No Parameter

Test it Now

Output:

I have nothing to say.

Java Lambda Expression Example: Single Parameter

Test it Now

Output:

Hello, Sonoo
Hello, Sonoo

Java Lambda Expression Example: Multiple Parameters

Test it Now

Output:

30
300

Java Lambda Expression Example: with or without return keyword

In Java lambda expression, if there is only one statement, you may or may not use return keyword. You must use return keyword when lambda expression contains multiple statements.

Test it Now

Output:

30
300

Java Lambda Expression Example: Foreach Loop

Test it Now

Output:

ankit
mayank
irfan
jai

Java Lambda Expression Example: Multiple Statements

Test it Now

Output:

I would like to say, time is precious.

Java Lambda Expression Example: Creating Thread

You can use lambda expression to run thread. In the following example, we are implementing run method by using lambda expression.

Test it Now

Output:

Thread1 is running...
Thread2 is running...

Java lambda expression can be used in the collection framework. It provides efficient and concise way to iterate, filter and fetch data. Following are some lambda and collection examples provided.

Java Lambda Expression Example: Comparator

Test it Now

Output:

Sorting on the basis of name...
2 Dell Mouse 150.0
1 HP Laptop 25000.0
3 Keyboard 300.0

Java Lambda Expression Example: Filter Collection Data

Test it Now

Output:

Iphone 6S: 65000.0
Sony Xperia: 25000.0
Redmi4 : 26000.0

Java Lambda Expression Example: Event Listener

Output:

Java Lambda Event Handling Example



Please Share

facebook twitter google plus pinterest

B.Tech / MCA


Learn Latest Tutorials