当前位置:网站首页>(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 - determine the bit number of harqack, csi1 and csi2 (Part II)
- 学习开发没有捷径,也几乎不存在带路会学的快一些的情况
- STM32 external interrupt experiment
- Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
- The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
- 51 MCU tmod and timer configuration
- NR PUCCH format0 sequence generation and detection mechanism
- [CSDN] C1 training problem analysis_ Part IV_ Advanced web
- 万字手撕七大排序(代码+动图演示)
- Education is a pass and ticket. With it, you can step into a higher-level environment
猜你喜欢
CEF download, compile project
[CSDN] C1 training problem analysis_ Part II_ Web Foundation
[CSDN] C1 training problem analysis_ Part IV_ Advanced web
在三线城市、在县城,很难毕业就拿到10K
51 MCU tmod and timer configuration
You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
UCI and data multiplexing are transmitted on Pusch - Part I
[combinatorics] Introduction to Combinatorics (combinatorial thought 2: mathematical induction | mathematical induction promotion | multiple induction thought)
Windows下MySQL的安装和删除
随机推荐
Stm32f04 clock configuration
Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~
[successful graduation] [1] - visit [student management information system]
51 MCU tmod and timer configuration
Working mode of 80C51 Serial Port
Successful graduation [3]- blog system update...
Timer and counter of 51 single chip microcomputer
Schematic diagram and connection method of six pin self-locking switch
QT qcombobox QSS style settings
Chromium Embedded Framework (CEF) 介绍
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
Sending and interrupt receiving of STM32 serial port
Serial port programming
2021-10-28
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
Interruption system of 51 single chip microcomputer
【男保姆式】教你打开第一个微信小程序