This lambda expression is an anonymous function. Simply put, this is a method without a declaration (without access modifiers that return a value and a name) .
Appeared in version 8 Java.
Usage example
Let's write a simple example of a functional interface :
public interface Lambda { //Метод интерфейса с отсутсвующей реализацией int getDoubleValue(int val); //Метод интерфейса с реализацией по-умолчанию default void printVal(int val) { System.out.println(val); } }
A functional interface should have only one abstract method. Read about the reasons for such a restriction here .
Now create a class to use.
public class ClassForLambda { public static void main(String[] args) { //Объявляем ссылку на функциональный интерфейс Lambda lam; //Параметр для нашего абстрактногго метода int num =9; //Прописываем первый вариант реализации lam = (val) -> val * 2; System.out.println(lam.getDoubleValue(num)); //Прописываем второй вариант реализации lam = (val) -> { System.out.println("Your number is "+val); return val * 2; }; System.out.println(lam.getDoubleValue(num)); } }
As you can see, the call to the method has not changed. Only the implementation has changed.
References: