Java 8 comes up with one great features called Lambda Expressions. It's first step to the functional programming. With Lambda expression we can treat functionality as method arguments i.e. you can pass a method as argument to another method.
Previously we use to write anonymous class if we wanted to pass some method as argument to another method but with lambda expression, we can pass a plain method as argument. Lambda expressions is a anonymous function i.e. It has arguments, a body and return type.
Syntax of Lambda Expression :
(Argument(s)) → {Body}
Eg :
(int x, int y, int z) - > {return x+y+z};
(String msg) - > {System.out.println(msg);}
() - > { return 100;}
Structure of Lambda Expression :
It can have zero, one or more number of parameters.
For empty set of parameters, Empty parentheses are used. e.g () -> 100
Type of the passed parameter can be explicitly declared or can be taken from context. e.g. (int x) is same as (x).
One important difference between anonymous inner class and lambda expression is, if we use ‘this’ it resolves to anonymous class but in the case of lambda expression, it resolves to the enclosing class where lambda is written.
Functional Interfaces :
A functional interface is an interface having one abstract method declared in it. It is similar to Marker interface just Marker interface has no methods or field declaration in it. java.lang.Runnable is one example of functional interface as It has only one method void run().
@FunctionalInterface :
It’s a new annotation which indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. As functional Interfaces can have only one abstract method, If you declare more than one then it will throws compile time error.
Eg :
Valid Functional interaface
@FunctionalInterface
public interface IFunctional1 {
public void display();
}
Invalid Functional interaface
@FunctionalInterface
public interface IFunctional {
public void display();
public void printResult();
}
It will throw compile time error as @functionalInterface contains two abstract methods.
Examples of Lambda Expression :
package in.techblog.waheed.lambda;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/**
* This class demostrate few examples of lambda expression
*
* @author abdul
*
*/
public class LambdaExample {
/**
* @param args
*/
public static void main(String[] args) {
// ****************Example1********************************//
// Old way of calling..
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Old way of calling..Anonymous.");
}
}).start();
// New way of calling....
new Thread(
() -> System.out
.println("New way of calling..Lambda Expression"))
.start();
// ****************************Example2******************//
List<Integer> list = Arrays.asList(101, 102, 103, 104, 105, 106, 107);
list.forEach(n -> System.out.print(" " + n));
System.out.println("\n");
// Double colon operator, Will cover in next tutorial..
list.forEach(System.out::print);
// *****************************Example3*****************//
// Print Even number
System.out.println("\n\nEven numbers:");
evaluate(list, (n) -> n % 2 == 0);
}
/**
* Evaluate the list on the basis of Predicate function. Check
* https://docs.oracle
* .com/javase/8/docs/api/java/util/function/Predicate.html
*
* @param list
* list of integer array that need to be evaluated
* @param predicate
* boolean-valued function
*/
public static void evaluate(List<Integer> list, Predicate<Integer> predicate) {
for (Integer l : list) {
if (predicate.test(l)) {
System.out.println(l + " ");
}
}
}
}
Please feel free to comment, Suggestion/Feedback are most welcome :)
References: