当前位置:网站首页>(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
- MySQL的简单使用(增删改查)
- NR technology -- MIMO
- C language enumeration type
- STM32 running lantern experiment - library function version
- JS foundation - prototype prototype chain and macro task / micro task / event mechanism
- The third paper of information system project manager in soft examination
- Which language should I choose to program for single chip microcomputer
- GPIO port details, Hal library operation keys
- Eight working modes of stm32gpio and chip naming rules
猜你喜欢
![[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer](/img/90/4de927e797ec9c2bb70e507392bed0.jpg)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer

万字手撕七大排序(代码+动图演示)

Idea remote breakpoint debugging jar package project

嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走

JMX、MBean、MXBean、MBeanServer 入门

How does the memory database give full play to the advantages of memory?

Blue Bridge Cup for migrant workers majoring in electronic information engineering

内存数据库究竟是如何发挥内存优势的?

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

UCI and data multiplexing are transmitted on Pusch (Part 4) --small block lengths
随机推荐
在三线城市、在县城,很难毕业就拿到10K
Synchronization control between tasks
2021-10-27
NR technology -- MIMO
uniapp 实现微信小程序全局分享及自定义分享按钮样式
STM32 external interrupt experiment
Serial communication based on 51 single chip microcomputer
el-table X轴方向(横向)滚动条默认滑到右边
Hal library sets STM32 clock
PRACH --- originator
[keil5 debugging] warning:enumerated type mixed with other type
[successful graduation] [1] - visit [student management information system]
Nr--- Pusch I: sorting out the agreement process
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
Timer and counter of 51 single chip microcomputer
2021-10-28
Definition and use of enum in C language
Getting started with JMX, MBean, mxbean, mbeanserver
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer