Java 8 - Lambda Expression with Examples
Java lambda expressions are the java's first step into functional programming like javascript and other functional programming languages.
Java before Lambda Expressions
We know very well that java is an object oriented language with an exception of primitives. Any function in java can not live on its own without belonging to some class. We always needed a class instance(object) to use a function in Java. We cannot define functions/methods which are independent so far. But now we have Labmda expressions to save us fro this problem, and solved the problem of functional programming for java.
What is Lambda Expression in java 8 ?
In simple words- Lambda expressions are the anonymous functions which can be created independently without belonging to any class. They can be passed as arguments,can be assigned to avariable and reduce the need of creating of anonymous class in java. Using lambda we can create a method in the same place where we are going to use it. Lmbda reduces the need of creating a method inside the class even if it is being used only once.
Example Without Lambda Expressions - Classsical Approach
Lets Try to uderstand the use of lambda expression wit a simple example -
Suppose we have an interface Readable with single abstract method read().package com.tutorials.scratcharound.java8.lambda; public interface Readable { void read(); }Now if we want to use the read() method , we have two options -
1. Create a class which implements the Readable interface and provide the implementation of read method.
2. Create an anonymous class and provide the method implementation to use it.
Both the above approaches are old without the use of lambda expression -
package com.tutorials.scratcharound.java8.lambda; public class Reader implements Readable { @Override public void read() { System.out.println("Yes i can read"); } }Now create an instance of Reader and call the read method -
package com.tutorials.scratcharound.lambda; public class HelloLambda { public static void main(String args[]){ Readable reader = new Reader(); reader.read(); } }OR create an anonymous class to call the read() method -
package com.tutorials.scratcharound.java8.lambda; public class HelloLambda { public static void main(String args[]){ Readable reader = new Readable() { @Override public void read() { System.out.println("Yes i also can read"); } }; reader.read(); } }
Example with Lambda Expressions
In both cases we will need to create a new object which is then used to call the read() method. But this same process can be done with much more ease by using java 8 Lambda Expressions.package com.tutorials.scratcharound.java8.lambda; public class HelloLambda { public static void main(String args[]){ Readable reader = () -> System.out.println("I can Read With More Ease"); reader.read(); } }This part is lambda expression in the above example -
() -> System.out.println("I can Read With More Ease"); In above example we have used Lambda Expression to declare the read() method without the need of creating any extra object.
How to write Lambda Expressions | Syntax
Lambda expressions are ususally written using the simple syntax -(arguments)->{body}
arguments are the arguments for that function
we can avoid curly braces if body of function contains single statement.
Some Examples -
//lambda with single parameter (s) -> System.out.println(s); //or could be written as s -> System.out.println(s); //lambda with no parameter () -> System.out.println("I can Read With More Ease"); //lambda with more than one parameter (a,b) -> System.out.println(a+b);Example: Iterating ArrayList using lambda Expression -
package com.tutorials.scratcharound.java8.lambda; import java.util.Arrays; import java.util.List; public class HelloLambda { public static void main(String args[]){ ListIn above example forEach method takes Consumer functional interface as argument. And we have passed a lambda exprssion as argument to it.list = Arrays.asList(1,2,4,3,2,55,6); list.stream().forEach(i->System.out.print(i)); } }
Functional Interface with Lambda Expression -
We have already discussed details about functional interface in our previous articles.Click here to see details.In brief an interface having only one abstract method is called functional interface. Because functional interfaces have only one method, lambda expression can be easily assigned to the functional interface, as we have alredy seen in the above examples.
Readable is an functional interface, and we used the following code of line -
Readable reader = () -> System.out.println("I can Read With More Ease");
So here it was a a breif introduction about lmbda exprssions in java 8. We will learn more about some other features of lambda expression and new features of java 8.
No comments :
Post a Comment