当前位置:网站首页>(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
边栏推荐
- UCI and data multiplexing are transmitted on Pusch (Part V) -- polar coding
- MySQL 数据库基础知识(系统化一篇入门)
- Basic knowledge of communication interface
- MySQL的简单使用(增删改查)
- For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
- 编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
- Raspberry pie installation SciPy
- 01仿B站项目业务架构
- Education is a pass and ticket. With it, you can step into a higher-level environment
- The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
猜你喜欢

2021-10-28

JS基础-原型原型链和宏任务/微任务/事件机制

The cyclic shift of PUCCH in NR channel is generated by MATLAB

Education is a pass and ticket. With it, you can step into a higher-level environment

There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way

STM32 serial communication principle

STM32 interrupt switch

在三线城市、在县城,很难毕业就拿到10K

编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解

03 FastJson 解决循环引用
随机推荐
CEF下载,编译工程
Education is a pass and ticket. With it, you can step into a higher-level environment
UCI and data multiplexing are transmitted on Pusch - determine the bit number of harqack, csi1 and csi2 (Part II)
Successful graduation [3]- blog system update...
[keil5 debugging] warning:enumerated type mixed with other type
STM32 serial port usart1 routine
2020-08-23
A lottery like scissors, stone and cloth (C language)
Mysql database underlying foundation column
STM32 port multiplexing and remapping
UCI and data multiplexing are transmitted on Pusch (Part V) -- polar coding
Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
Intelligent home design and development
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
01仿B站项目业务架构
开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~
Nr-prach:prach format and time-frequency domain
uniapp 实现微信小程序全局分享及自定义分享按钮样式
Eight working modes of stm32gpio and chip naming rules