当前位置:网站首页>(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
边栏推荐
- Schematic diagram and connection method of six pin self-locking switch
- Fundamentals of Electronic Technology (III)__ Fundamentals of circuit analysis__ Basic amplifier operating principle
- Simple use of MySQL (addition, deletion, modification and query)
- 一个可执行的二进制文件包含的不仅仅是机器指令
- SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
- MySQL的简单使用(增删改查)
- GPIO port details, Hal library operation keys
- Introduction to chromium embedded framework (CEF)
- For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
- Matlab reads hexadecimal numbers and converts them into signed short
猜你喜欢
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
Stm32-hal library learning, using cubemx to generate program framework
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
内存数据库究竟是如何发挥内存优势的?
Stm32f407 key interrupt
UCI and data multiplexing are transmitted on Pusch - Part I
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
C language enumeration type
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
随机推荐
Nr-prach:prach format and time-frequency domain
2021-01-03
[22 graduation season] I'm a graduate yo~
For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
MySQL 数据库基础知识(系统化一篇入门)
[graduation successful] [1] - tour [Student Management Information System]
STM32 port multiplexing and remapping
Not many people can finally bring their interests to college graduation
Definition and use of enum in C language
自动装箱与拆箱了解吗?原理是什么?
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
STM32 serial port usart1 routine
Serial communication based on 51 single chip microcomputer
Introduction to chromium embedded framework (CEF)
Project cost management__ Cost management technology__ Article 6 prediction
2021-10-27
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
Synchronization control between tasks
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode