Java 8 : Lambda Example
Lambda Expressions:
Lambda expressions are new and important feature of Java 8. They provide a clear way to represent one method interface using lambda expression. And they provide easy way to access Collection libraries for iterating, filter and extract data.
Example :
Syntax : (arg1, arg2) -> {body}
For Ex:
(int a, int b) -> { return a + b; } () -> System.out.println("Hello World"); (String s) -> { System.out.println(s); } //Old way of iterating Collection List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7); for(Integer n: list) { System.out.println(n); } //New way with lambda: List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7); list.forEach(n -> System.out.println(n));
Rules:
- It can have zero or more number of arguments.
- Parameters are separated by using comma
- Empty parenthesis are used to represent empty parameters eg : () -> System.out.println(“Hello World”);
- Curly braces for body is not mandatory if it contains only single statement, other wise it should enclosed with curly braces.
Program:
interface TestInterface{ void display(); } public class TestInterfaceImpl{ public static void main(String[] args){ TestInterface li = new TestInterface(){ public void display(){ System.out.println("Hello World"); } }; li.display(); } }
Here we used anonymous class to implement TestInterface and this interface has only one method, by using lambda expression without using anonymous class we can call the display() method
interface TestInterface{ void display(); } public class TestInterfaceImpl{ public static void main(String[] args){ //Old way: TestInterface old = new TestInterface(){ public void display(){ System.out.println("Old : Display method called!"); } }; old.display(); //New way with Lambda expression TestInterface li = () -> System.out.println("New : Display method called !"); li.display(); } }
This approach we regularly use in Java Swing, while adding an action to Swing Component we use ActionListener actionPerformed() method, ActionListener is an interface which has only one method called actionPerformed(),
//old way: button.addActionListener(new ActionListener()){ public void actionPerformed(ActionEvent e){ System.out.println("The button was clicked using old fashion code!"); } } //New way: button.addActionListener( (e) -> { System.out.println("The button was clicked. From lambda expressions !"); });
java.util.Runnable also has only one method public void run(), this also we can represent using lambda
//old way Runnable r = new Runnable(){ public void run(){ System.out.println("Runnable run method"); } } //New way with lambda expression Runnable r = ()->System.out.println("Runnable run method"); //So we can create Thread object like below Thread t = new Thread(()->{System.out.println("Runnable run method from lambda")});
Any interface which has only one method can be used with lambda expression instead of old anonymous inner class.
Thanks for reading.
can you please explain what is Lambda Expression is simple English so that it is easy to learn the concept first
Hello mate nicee post