当前位置:网站首页>(1) What is a lambda expression
(1) What is a lambda expression
2022-07-03 09:58:00 【look-word】
1 What is? Lambda expression
Runnable Interface
@FunctionalInterface // The interface modified by this annotation can only declare one abstract method
public interface Runnable {
public abstract void run();
}
The code structure
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(" Code executed in a new thread :"+Thread.currentThread().getName());
}
}).start();// Not with lambda Writing method of creating thread
Take the above code as an example :
- We all know You cannot call the interface directly To touch is the class implementation interface Then instantiate the calling method
- Or use it directly Anonymity class class The way Lambda Is to simplify the writing of anonymous classes and classes
- The traditional way It is generally used The above code Or similar
Use Lambda simplify
Of course, it can also be more simplified It will be described in detail below
new Thread(() ->{
System.out.println(" Code executed in a new thread :"+Thread.currentThread().getName());
}).start();
2 Lambda Grammatical rules
Lambda No object-oriented rules ,Lambda The standard format for is provided by 3 Component composition :
( Parameter type Parameter name ) -> {
Code body ;
}
Format specification :
- ( Parameter type Parameter name ): parameter list
- { Code body ;} : Method body
- ->: arrow , Split parameter list and method body
Simple exercises :
- No arguments
public class Demo1Lambda {
public static void main(String[] args) {
// Start a new thread
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(" Code executed in a new thread :"+Thread.currentThread().getName());
}
}).start();// Not with lambda Writing method of creating thread
new Thread(() ->{
// Call a parameterless method
System.out.println(" Code executed in a new thread :"+Thread.currentThread().getName());
}).start();
System.out.println(" Code in the main thread :" + Thread.currentThread().getName());
}
}
3 @FunctionalInterface annotation
/** * @FunctionalInterface * This is a logo annotation , The interface modified by this annotation can only declare one abstract method * Multiple compilers fail */
@FunctionalInterface
public interface UserService {
void show();
}
4 Lambda The omission of expressions
stay lambda Based on the standard writing of expressions , The rule that you can use ellipsis is :
- Parameter types in parentheses can be omitted
- If there is only one parameter in the parenthesis , Then brackets can be omitted
- If there is and only one statement in braces , You can omit braces at the same time ,return Keywords and statement semicolons .
Interface
public interface StudentService {
String show(String name, Long id) ;
}
Code implementation
public static void main(String[] args) {
goStudent((String name,Long id) ->{
return name+id;
});
// lambda Simplified code
goStudent((String name,Long id) -> name+id+666);
}
// Custom methods
public static void goStudent(StudentService studentService){
System.out.println(studentService.show(" Zhang San ", 22L));
}
- Generally, we will base on the readability of the code Moderate simplification
5 Lambda The premise of expression
Lambda The syntax of expressions is very simple , however Lambda Expressions are not random , There are several conditions to pay special attention to when using
It means
- The parameter or local variable type of a method must be an interface to use Lambda
- There is and only one abstract method in the interface (@FunctionalInterface)
6 Lambda Compared to anonymous inner classes
Lambda Compared to anonymous inner classes
- The types of scenarios used are different
- The type of anonymous inner class can be class , abstract class , Interface
- Lambda The type required for an expression must be an interface
- The number of abstract methods is different
- The number of abstract methods in the interface required by anonymous inner classes is arbitrary
- Lambda An expression requires only one abstract method in the interface
- The implementation principle is different
- Anonymous inner classes are compiled to form a class
- Lambda The expression is when the program is running Dynamic generation class
边栏推荐
- STM32 port multiplexing and remapping
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- 在三线城市、在县城,很难毕业就拿到10K
- 内存数据库究竟是如何发挥内存优势的?
- UCI and data multiplexing are transmitted on Pusch - placement of data and UCI positions (Part III)
- GPIO port details, Hal library operation keys
- 对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- Timer and counter of 51 single chip microcomputer
- [Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
猜你喜欢

单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇

Stm32f407 key interrupt

The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
![[successful graduation] [1] - visit [student management information system]](/img/91/72cdea3eb3f61315595330d2c9016d.png)
[successful graduation] [1] - visit [student management information system]

UCI and data multiplexing are transmitted on Pusch - Part I

Interruption system of 51 single chip microcomputer

Windows下MySQL的安装和删除

2021-10-28

03 FastJson 解决循环引用

STM32 serial port usart1 routine
随机推荐
The third paper of information system project manager in soft examination
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
Development of fire evacuation system
Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
没有多少人能够最终把自己的兴趣带到大学毕业上
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
2020-08-23
Liquid crystal display
[male nanny style] teach you to open the first wechat applet
一个可执行的二进制文件包含的不仅仅是机器指令
(2)接口中新增的方法
Stm32f407 key interrupt
Project scope management__ Scope management plan and scope specification
There is no specific definition of embedded system
C language enumeration type
Introduction to chromium embedded framework (CEF)