Lambda expressions or functions are nameless functions i.e just as anonymous function with no name.
Java earlier being only OOP language there was no existense of functions outside the object or instance. But since java 8 functional programming was introduced you can define functions, give them reference variables or pass them as method arguments and more.
Lambda syntax :
(x,y) -> x+y
1. (parameters) -> expression
2. (parameters) -> {statements;}
3. () -> expression
Some features of Lambda Expressions :
- A lambda expression can have zero, one or more parameters.
- When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses. e.g. a -> return a*a.
- The body of the lambda expressions can contain zero, one or more statements. If body of lambda expression has single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression. When there is more than one statement in body than these must be enclosed in curly brackets.
Lambda relationship with Functional interfaces
SAM-Single Abstract Method Inferfaces also called as functional interfaces, java 8 enforces the rule of SAM by annotating these interfaces with a new annotation i.e @FunctionalInferface
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
If you try to add a new method in any functional interface, compiler would not allow you to do this and will throw compile time error.
So, How are they related ?
Let us see an example to find the relation, there is a thread which prints “Test Functional Interface”
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Test Functional Interface");
}
}).start();
When we use lambda here , above code equivalent lambda is like this :
new Thread(
() -> {
System.out.println("My Runnable run method");
}
).start();
In simple words, a lambda expression is an instance of a functional interface.
Examples : Java 8 lambda expression
1. Iterating over a List and perform some operations
List<String> list = new ArrayList();
list.add("1");
list.add("2");
list.forEach(p -> {
System.out.println(p);
//Do more work
}
);
2. Create a new runnable and pass it to thread
new Thread(
() -> System.out.println("My Runnable");
).start();
3. Sorting employees objects by their name
public class LambdaIntroduction {
public static void main (String[] ar){
Employee[] employees = {
new Employee("David"),
new Employee("Naveen"),
new Employee("Alex"),
new Employee("Richard")};
System.out.println("Before Sorting Names: "+Arrays.toString(employees));
Arrays.sort(employees, Employee::nameCompare);
System.out.println("After Sorting Names "+Arrays.toString(employees));
}
}
class Employee {
String name;
Employee(String name) {
this.name = name;
}
public static int nameCompare(Employee a1, Employee a2) {
return a1.name.compareTo(a2.name);
}
public String toString() {
return name;
}
}
Output:
Before Sorting Names: [David, Naveen, Alex, Richard]
After Sorting Names [Alex, David, Naveen, Richard]
Summary
In this article we learnt about Java 8 lambda expression, we learnt its syntax, its features, lambda with functional interface and its various other implementations with examples, try it out.
Hope you liked the article !