✌️✌️✌️ Before writing , I'm considering whether to write this article , But when I look up “lambda expression ” When the relevant content of , I found that a large number of articles are redundant , The length of the article is too exaggerated , It has seriously affected everyone's interest in reading , So I decided to write an article about lambda Expression article , So that everyone can be right to a certain extent lambda Have a certain understanding of expressions . I hope I can help you in Java Play a certain role in learning .

 

 

Lambda summary

Lambda The expression is JDK8 A new grammatical form after the beginning

effect : Simplify the code writing of anonymous inner classes

format : 
( The formal parameter list of the overridden method of the anonymous inner class )->{ Method body code of the overridden method } notes :-> It's a grammatical form , No practical significance

First, we define an abstract class , Override its methods through anonymous inner classes , Then simplify it , Compare the changes between the two :

use Lambda Before expression : 
public static void main (String[] args){ People p = new People(){ @Override
public void run(){ System.out.println(" Pupils run very slowly "); } }; p.run(); } abstract
class People{ public abstract void run(); }
use Lambda After expression :
People p = ()->{ System.out.println(" Pupils run very slowly "); }; // An error will be reported
An error will be reported , Why ? The reason is that the previous Lambda The definition of the expression is not clear ,Lambda Expressions cannot be simplified, so anonymous inner classes ,
It can only simplify the writing form of anonymous inner classes of functional interfaces , And there is only one abstract method in the interface
, usually , We'll add another one to the interface @Functionalinterface annotation , Mark that the interface must be a functional interface .

 

Pass in the real sense Lambda Expression simplification : 
@FunctionalInterface// This annotation must be a functional interface , And there is only one abstract method interface People{ void run(); }
// Simplify previous anonymous inner classes People p = new People(){ @Override public void run(){
System.out.println(" Pupils run very slowly !"); } }; // After simplification People p = () -> {
System.out.println(" Pupils run very slowly !"); };
in addition , It can also be used as a parameter in the method :
// Define one based on the original pk method public static void pk(People p){
System.out.println(" How was your run :") p.run(); } // Not used Lambda In case of :
pk(p);// After defining the anonymous inner class , take p Substitute in ; // use Lambda Situation : People p = () -> {
System.out.println(" Pupils run very slowly !"); }; pk(() -> { System.out.println(" Pupils run very slowly !");
}); // Because the right side of the equal sign is People Created object p, Therefore, it can be directly substituted into

Lambda Benefits of :

 Lambda Is an anonymous function , We can put Lambda An expression is understood as a piece of code that can be passed , It can write more concise , More flexible code , As a more compact code style , send Java The ability of language expression has been improved .

 

 

Lambda Role of actual combat  

In the previous learning process , We've studied Arrays Relevant knowledge of , One of them sort method ( Arrays can be arranged in ascending or descending order ), In fact, the interface is also a class , And there is only one abstract method :
// Before streamlining : Arrays.sort(grade,new Comparator<Integer>(){ @Override public int
compare(Integer o1,Integer o2){ return o2 -o1; } }); // After streamlining :
Arrays.sort(grade,(Integer o1,Integer o2) -> { return o2 -o1; });
 

Lambda Expression ellipsis rule  

* Parameter types can be omitted and not written // Refer to the previous practical examples Arrays.sort(grade,(Integer o1,Integer o2) -> {
return o2 -o1; }); // After omitting parameter type Arrays.sort(grade,( o1, o2){ return o2 -o1; });
* If there is only one parameter , Parameter types can be omitted , meanwhile () It can also be omitted // Simply reference an example of a single parameter , You don't need to know what it means
btn.addActionListener((ActionEvent e) -> { System.out.println(" I'm a simple example ."); });
// After simplification : btn.addActionListener( e -> { System.out.println(" I'm a simple example ."); });
* If Lambda The method body code of the expression has only one line of code . Braces can be omitted , Also omit the semicolon // Refer to the example of the previous rule
btn.addActionListener( e -> { System.out.println(" I'm a simple example ."); }); // After simplification :
btn.addActionListener( e -> System.out.println(" I'm a simple example .") );
*
If Lambda The method body code of the expression has only one line of code . Braces can be omitted . here , If this line of code is return sentence , Must be omitted return Don't write , It must also be omitted ";" Don't write
// Refer to the code of the first rule Arrays.sort(grade,(Integer o1,Integer o2) -> { return o2 -o1; });
// After simplification : Arrays.sort(grade,( o1, o2)-> o2 -o1);

 

Creation is not easy , Give me a third company  

Technology
©2019-2020 Toolsou All rights reserved,
【C++ Must see for entry 】C++ from 0 reach 1 Introductory programming axios Interceptor packaging and use Spring Boot Interview must ask : Automatic configuration principle VMware 16 install centos 7 Detailed tutorial C Language data structure - Sequence table delete duplicates V2.0.0 The 12th Blue Bridge Cup c++b Group personal problem solving On sending data from serial port single chip microcomputer to upper computer centos7 install RabbitMqjava Polymorphic array of opencv-python Fourier transform and inverse transform