Lambda expression
- Eclipse: Oxygen
- Java: 1.8
The lambda expression is used mainly to execute programs making the programs shorter. Therefore, previously we used classes to implement functions defined in the interfaces, but here we can directly write the function declarations with the help of the lambda expression.
In the following program, we demonstrate how to create a lambda expression with a single statement.
To create a lambda expression, we specify the input parameters (if any) on the left side of the lambda operator ->, we place the expression of the declarations on the right side of the lambda operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
interface Lambda{ public void demo(); } public class App { public static void main(String[] args) { Lambda lambda = ()-> System.out.println("Statement 1"); lambda.demo(); } } |
Output
1 |
Statement 1 |
In the following program, we demonstrate how to create lambda expressions with multiple statements.
To create multiple lambda expressions, we use curly braces {}, and the return keyword is required if the lambda expression is expected to return a value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
interface Lambda{ public void demo(); } public class App { public static void main(String[] args) { Lambda lambda = ()->{ System.out.println("Statement 1"); System.out.println("Statement 2"); }; lambda.demo(); } } |
Output
1 2 3 |
Statement 1 Statement 2 |
Lambda expression continues
We can make use of lambda expression for functional type interfaces only. Â An interface with only a single abstract method is called a functional interface.
Here the lambda expression on threads is explained. As we know, we can have created threads with the Runnable method and, therefore, this could be done more efficiently with the help of lambdas.
The following program demonstrates the Runnable interface without using the Lambda expression.
1 2 3 4 5 6 7 8 9 10 11 |
public static void main(String[] args) { Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("I am inside thread 1"); } }); thread1.start(); } |
Output
I am inside thread 1
The following program demonstrates the Lambda expression, instead of creating a Runnable interface, we can create a lambda expression inside the main method.
1 2 3 4 5 6 7 8 9 10 |
public class App { public static void main(String[] args) { new Thread(()->{ System.out.println("I am inside thread 1"); }).start();; } } |
Output
I am inside thread 1
Here is the same example of a lambda expression with multiple statements.
1 2 3 4 5 6 7 8 9 10 11 |
public class App { public static void main(String[] args) { new Thread(()->{ System.out.println("I am inside thread 1"); System.out.println("This is line 2"); }).start();; } } |
Output
I am inside thread 1
This is line 2
Lambda expressions, something more
The lambda expression is explained here with the Collections in java. AÂ couple of methods such as the compareTo method and many more are written shortly and efficiently with the help of lambda expression.
Here are some lambda and collection examples, which demonstrate how to use a lambda expression in place of the comparator object. It provides an efficient and concise way to iterate, and fetch data.
We can use the Comparator interface for sorting elements of the list, it contains one abstract method called compare() method.
This is an example of sorting a collection with a comparator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
class Data { private String name; public Data(String name) { super(); this.name = name; } public String getName() { return name; } @Override public String toString() { return "Data [name=" + name + "]"; } } public class App { public static void main(String[] args) { List<Data> list = new ArrayList<>(); list.add(new Data("Chaand")); list.add(new Data("Ed")); list.add(new Data("Angelica")); list.add(new Data("Maqbul")); list.add(new Data("John")); Collections.sort(list, new Comparator<Data>() { @Override public int compare(Data o1, Data o2) { return o1.getName().compareTo(o2.getName()); } }); for (Data data : list) { System.out.println(data.getName()); } } } |
The following program demonstrates how to iterate lambda expression over a List which helps us to concise our program.
Here we are using a lambda expression in place of comparator object for defining our sorting in collections. To create multiple lambda expressions, we use curly braces {} and use return keyword.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class Data { private String name; public Data(String name) { super(); this.name = name; } public String getName() { return name; } @Override public String toString() { return "Data [name=" + name + "]"; } } public class App { public static void main(String[] args) { List<Data> list = new ArrayList<>(); list.add(new Data("Chaand")); list.add(new Data("Ed")); list.add(new Data("Angelica")); list.add(new Data("Maqbul")); list.add(new Data("John")); Collections.sort(list, (Data o1, Data o2) -> { return o1.getName().compareTo(o2.getName()); }); for (Data data : list) { System.out.println(data.getName()); } } } |
Output
Angelica
Chaand
Ed
John
Maqbul
The following program demonstrates the use of lambda expression to perform some custom operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
class Data { private String name; public Data(String name) { super(); this.name = name; } public String getName() { return name; } @Override public String toString() { return "Data [name=" + name + "]"; } } public class App { public static void main(String[] args) { List<Data> list = new ArrayList<>(); list.add(new Data("Chaand")); list.add(new Data("Ed")); list.add(new Data("Angelica")); list.add(new Data("Maqbul")); list.add(new Data("John")); Collections.sort(list, (Data o1, Data o2) -> { if(o1.getName().length() < o2.getName().length()){ return -1; }else if(o1.getName().length() > o2.getName().length()){ return 1; }else{ return 0; } }); for (Data data : list) { System.out.println(data.getName()); } } } |
Output
Ed
John
Chaand
Maqbul
Angelica
Lambda expressions, few more thing
This method is a descriptive summary of all the things we have covered so far in the previous module. In the following sections, you will learn how variables work within lambda expressions.
The body of a lambda expression has the same scope as the Anonymous class.
The following program demonstrates lambda expression for a single parameter.
Here method variable will be able to access inside the lambda.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
interface Lamda { abstract public void demo(int x); } public class App { public static void main(String[] args) { Lamda lamda = (int x) -> { System.out.println("The value of x is:" + x); System.out.println("Statement from demo method"); }; lamda.demo(201); } } |
Output
The value of x is: 201
Statement from demo method
This program demonstrates lambda expression for two parameters. This expression implements the Lambda interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
interface Lamda { abstract public void demo(int x, float y); } public class App { public static void main(String[] args) { Lamda lamda = (int x, float y) -> { System.out.println("The value of x is :" + x + " and the value of y is: " + y); System.out.println("Statement from demo method"); }; lamda.demo(201, 503.12f); } } |
If we have only one statement in our method then we place the expression of the declarations on the right side of the lambda operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
interface Lamda { abstract public int demo(); } public class App { public static void main(String[] args) { Lamda lamda = () -> 10; /* * { System.out.println("Statement 1"); return 10; }; */ System.out.println(lamda.demo()); } } |
With the help of lambda expression, you can perform an operation. For example Lamda lamda = () -> (10*25);
Lambda expressions with variables and iterations
The use of lambda in foreach loop is shown and how it is efficiently used than the traditional foreach loop is given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class Data{ private String name; public Data(String name) { super(); this.name = name; } @Override public String toString() { return "Data [name=" + name + "]"; } public String getName() { return name; } } public class App { public static void main(String[] args) { List<Data> list = new ArrayList<>(); list.add(new Data("Chaand")); list.add(new Data("John")); list.add(new Data("Mia")); list.add(new Data("Angelica")); list.add(new Data("Roy")); list.forEach(temp->{ System.out.println(temp.getName()); }); } } |
Output
Chaand
John
Mia
Angelica
Roy
Contributed by: Poonam Tomar