<>Lambda expression :

<>1, brief introduction

first Lambda The expression belongs to Java8 of
A new feature , provide Java Support for functional programming in programming , Contribute to the simplicity of the code , Can replace most anonymous functions , Especially for the traversal and operation of sets , Greatly simplifies the code .

Lambda Body of expression :

Functional interface :

be careful :
Lambda Expressions must be used with functional interfaces , The so-called functional interface , That is, the interface with only one abstract method in the interface is a functional interface , We can customize it ,JDK A large number of functional interfaces are also built in .

1,@FunctionalInterface Annotations decorate the interface , Then this interface is a functional interface , There can only be one way , The following is a functional interface :
@FunctionalInterface public interface MyInteface { void eat(); }
2, If not @FunctionalInterface** annotation , You only write an abstract method in the interface, which can also be considered as a functional interface :
public interface MyInteface { void eat(); }
This is also possible .

3, There is only one case of functional interfaces, not just abstract methods , That is inheritable Object Class method :
@FunctionalInterface public interface MyInteface3 { void eat(); @Override
StringtoString(); @Override int hashCode(); }
<>2,Lambda Use of expressions :

1, Use in common methods

Student class :
@FunctionalInterface public interface Student { void eat(); }
Test class :
public class Test { public static void main(String[] args) { Student stu = new
Student() { // Common method , Rewrite and use @Override public void eat() { System.out.println(" I am a student "
); } }; stu.eat(); //lambda Expression writing :
// parameter 1: Rewritten Student The only parameterless in the interface eat The abstract method is implemented in detail , So rewriting is not Signature required // parameter 2:-> expression changeless
// parameter 3:{ Specific implementation } yes Student Unique in the interface eat The method is implemented in detail Student stu2 = () -> { System.out.
println(" Students eat "); }; stu2.eat(); } }
output :
I am a student Students eat
2, Use of parametric methods

Student class :
@FunctionalInterface public interface Student { void eat(String food); }
Test class :
public class Test { public static void main(String[] args) {
//lambda rewrite Student The only parameterized method of the interface : Student stu2 = (foodName)->{ System.out.println(
" The students are eating "+foodName); }; stu2.eat(" meat "); } } // output : The students are eating meat
<>3,Lambda Expression implementation multithreading

Previously in multithreading (1) The article in introduced the method of creating multithreading , Use it here lambda To create a thread :
public class Test { public static void main(String[] args) { Thread t = new
Thread(() -> { System.out.println(" This thread is created by lambda To create "); }); t.start(); } }
<>4,Lambda Expression operation

We use lambda To operate the operation can reduce a lot of code :

Functional interface :
@FunctionalInterface public interface Calculator<T> { T operation(T v1,T v2); }
Test class :
public class Test { // computing method public static Integer operator(Integer v1,Integer v2
,Calculator<Integer> calculator){ return calculator.operation(v1, v2); } public
static void main(String[] args) { // use lambda expression : // The meaning here is to pass in two parameters , Return the value after running int add
= Test.operator(5,10,(x,y)->{ return x+y; }); // Abbreviation : You can write a lot less code , More brief than above int num1 =
Test.operator(5,10,(x,y)->x+y); int num2 = Test.operator(10,5,(x,y)->x-y);
System.out.println(add); System.out.println(num1); System.out.println(num2); } }
output :
15 ,15 ,5
<>5,Lambda Expression method reference

Sometimes we don't have to rewrite the methods of the interface to do specific implementation , If we have a way to realize it , You can also use this method
Reference the existing methods to implement the methods in the interface , This benefit is code reuse , Like the following :

Functional interface :
public interface ResultOneParam { int method(int a); }
Test class :
public class Test { public int addTo(int a){ return a+10; } public static int
addTo2(int a){ return a+10; } public static void main(String[] args) {
//lambda Rewritten method method ResultOneParam lambda1=(a)->a+10;
// Method reference : Is in Test Inside addTo2 Method to replace method Rewritten method ResultOneParam lambda2= Test::addTo2;
int result1= lambda2.method(9); System.out.println(result1); // Method reference
:: Reference an existing method to replace method overrides , This allows method reuse Test test=new Test(); ResultOneParam lambda3=test::
addTo; int result2= lambda3.method(9); System.out.println(result1); } }
<>6,Lambda Use of expressions for collections

of course Lambda The operation of the set is also very convenient , You can have a lot less code :
public class Test { public static void main(String[] args) { List<Integer> list
= Arrays.asList(5,1,3,4,5,0,9,7,0,1,5); //lambda Expression traversal collection , Rewritten Consumer Interface method list.
forEach((element)->{ System.out.println(element); }); // Abbreviation : list.forEach(element
-> System.out.println(element)); //lambda Expression method reference , Used to traverse the output list aggregate : list.forEach(
System.out::print); // output list Even number of : list.forEach(element->{ if(element%2==0){
System.out.println(element); } }); } }
<>7, summary

All right
The above is Lambda Some basic uses of expressions , This too Java8 A new feature of , As for the principle and other methods , You can refer to other technical blogs ,Lambda The appearance of greatly simplifies the code , If you can use it skillfully lambda To the actual business , The overall cleanliness of the code has been greatly improved !

Technology