lambda expression introduction
The lambda expression introduced by java 8 simplifies our code by replacing anonymous internal classes, which are essentially anonymous classes.
Simple use of lambda expressions
Here's how we implement an interface using an anonymous internal class:
public class Main { public static void main(String[] args) { //Implementing interfaces using anonymous internal classes Cal cal = new Cal(){ @Override public int test(int a, int b){ return a + b; } }; System.out.println(cal.test(1, 2)); } } interface Cal{ //No parameters no return type int test(int a, int b); }
But if we use a lambda expression, it will take the following form:
public class Main { public static void main(String[] args) { //Implementing interfaces using lambda expressions Cal cal1 = (a, b) -> a + b; System.out.println( cal1.test(1, 2)); } } interface Cal{ //Addition Method int test(int a, int b); }
As we can see from the above, using lambda expressions can greatly simplify our code, but it can also be difficult to read. Let's take a closer look at lambda expressions.
Syntax for lambda expressions
The expression syntax of lambaa is as follows:
(int a, int b) -> {return a + b;);
It is essentially a method.
The general methods are as follows:
int add(int a, int b){ return a + b; }
We can see that the general methods are return values, method names, parameter lists, and method bodies.
lambda expressions only have parameter lists and method bodies
(int a, int b) -> {};
(): Used to describe parameter list
->: An operator called a lambda expression, which can be called an arrow symbol
{}: Used to describe method body
lambda expressions with six different parameter lists and different return types
The six different parameter lists and return types are:
-
No parameters no return type
-
1 parameter has no return type
-
Two parameters have no return type
-
No parameter return type
-
1 Parameter return type
-
2 parametric return types
public class Main { public static void main(String[] args) { //No parameters no return type Cal cal = ()->{ System.out.println("No parameters no return type"); }; cal.test(); //1 parameter has no return type Cal1 cal1 = (int a) -> { System.out.println("1 Parameters have no return type"); }; cal1.test(1); //Two parameters have no return type Cal2 cal2 = (int a, int b) -> { System.out.println("2 Parameters have no return type"); }; cal2.test(1, 2); //No parameter return type Cal3 cal3 = ()->{ System.out.println("No parameter return type"); return 1; }; System.out.println(cal3.test()); //1 Parameter return type Cal4 cal4 = (int a) -> { System.out.println("1 Parameter Return Types"); return a; }; System.out.println(cal4.test(1)); //2 parametric return types Cal5 cal5 = (int a, int b) -> { System.out.println("2 Parameter Return Types"); return a + b; }; System.out.println(cal5.test(1, 2)); } } interface Cal{ //No parameters no return type void test(); } interface Cal1{ //1 parameter has no return type void test(int a); } interface Cal2{ //Two parameters have no return type void test(int a, int b); } interface Cal3{ //No parameter return type int test(); } interface Cal4{ //1 Parameter return type int test(int a); } interface Cal5{ //2 parametric return types int test(int a, int b); }
It is easy to understand when using lambda expressions as anonymous internal classes.
Refinement of lambda expression
Grammatical Notes:
- Parameter types can be omitted
- If there is only one parameter, () can be omitted
- If there is only one statement in the body of the method and the statement is not a return statement, {} can be omitted
- If there is only one statement in the body of the method and the statement is a return statement, omit the braces and return as well
The code examples are as follows:
public class Main { public static void main(String[] args) { //1 parameter has no return type Cal1 cal1 = a -> System.out.println("1 Parameters with no return type"); //Omit (), parameter type, {} //1 parameter has a return type Cal2 cal2 = a -> a; //Omit (), parameter type, {}, return cal1.test(1); System.out.println(cal2.test(2)); } } interface Cal{ //No parameters no return type int test(int a, int b); } interface Cal1{ //1 parameter has no return type void test(int a); } interface Cal2{ //1 Parameter return type int test(int a); }
Method Reference
Sometimes multiple lambda expressions are implemented in the same way, and we can encapsulate them as generic methods for ease of use.
If it is a non-static method, the format is object name:
If it is a static method. Format as class name:Method
The code examples are as follows:
public class Main { public static void main(String[] args) { Main m = new Main(); Cal2 cal2 = Main::test1; System.out.println(cal2.test(1)); } public static int test1(int a){ return a; } } interface Cal{ //No parameters no return type int test(int a, int b); } interface Cal1{ //1 parameter has no return type void test(int a); } interface Cal2{ //1 Parameter return type int test(int a); }
Construction Method Reference
If a functional interface can be implemented using the construction method of a class, then it can be referenced using the construction method
Format as
- Class name:new
Code examples:
public class Main { public static void main(String[] args) { Service s = Dog::new; System.out.println(s.getDog()); Service2 s2 = Dog::new; System.out.println(s2.getDog("millet", 20)); } } class Dog{ private String name; private int age; public Dog(String name, int age) { this.name = name; this.age = age; } public Dog() { } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + '}'; } } interface Service{ Dog getDog(); } interface Service2{ Dog getDog(String name, int age); }
Traversal and sorting of sets by lambda expressions
The code examples are as follows:
public static void main(String[] args) { List<Dog> list = new ArrayList<>(); list.add(new Dog("all around", 24)); list.add(new Dog("Erming", 22)); list.add(new Dog("Sanming", 23)); list.add(new Dog("Daming", 21)); //lambda's sorting of collections list.sort((a, b) -> a.getAge() - b.getAge()); //lambda's traversal over a set list.forEach(System.out::println); } } class Dog{ private String name; private int age; public Dog(String name, int age) { this.name = name; this.age = age; } public Dog() { } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Functional Interface
If an interface has and only has one abstract method, we call it a functional interface. lambda expressions can only be used with functional interfaces.
- If an interface has only one abstract method, it is a functional interface
- java provides the @FunctionalInterface annotation, and if it is used on an interface, it will be required as a functional interface, and error will occur if it does not meet the requirements of a functional interface.
@FunctionalInterface interface Cal{ int add(); }