当前位置:网站首页>Lambda expression
Lambda expression
2022-07-03 12:17:00 【Early summer 0811】
One 、 introduce lambda expression
Let's create a new thread , Then implement runable Interface run Method :
public class demo1 {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(" The new thread executes ...");
}
}).start();
}
}
If you use lambda The expression can be abbreviated as :
public class demo1 {
public static void main(String[] args) {
new Thread(() -> System.out.println("lambda The expression was executed ...")).start();
}
}
labmda Benefits of expressions :
1. You can simplify anonymous inner classes , Simplify the code
Two 、lambda The standard format of expressions
( parameter list ): parameter list
{}: Method body
-> : No special significance , Connect parameters and method bodies
1. No parameter no return value
public interface Swimingable {
void swiming();
}
public class Swiming {
public static void main(String[] args) {
goSwiming(() ->{
System.out.println(" I am a lambda swimming ");
});
}
public static void goSwiming(Swimingable swimingable){
swimingable.swiming();
}
}
Run the following :
I am a lambda swimming
Seeing that the parameter of the method is an interface, you can consider using lambda expression , But not all anonymous inner classes can be used ;ambda expression ,lambda Expressions are equivalent to rewriting interfaces .
1. With parameter and return value
public interface SmokeAble {
int smoke(String name);
}
public class Smoking {
public static void main(String[] args) {
goSmoking((String name) -> {
System.out.println("labmda I smoked today " + name + " The smoke of ");
return 5;
});
}
public static void goSmoking(SmokeAble s) {
int i = s.smoke(" Yellow Crane Tower ");
System.out.println(" Return value :" + i);
}
}
The operation results are as follows :
labmda Today, I smoked from the Yellow Crane Tower
Return value :5
Let's use lambda Implement sorting :
public class PersonLambda {
public static void main(String[] args) {
List<Persion> persions = new ArrayList<>();
persions.add(new Persion(" Lau Andy ", 55, 167));
persions.add(new Persion(" Jacky Cheung ", 58, 174));
persions.add(new Persion(" early summer ", 42, 180));
Collections.sort(persions, (Persion o1, Persion o2) -> {
return o1.getAge() - o2.getAge();
});
for (Persion persion:persions){
System.out.println(persion);
}
}
}
Run the following :
Persion{
name=' early summer ', age=42, height=180}
Persion{
name=' Lau Andy ', age=55, height=167}
Persion{
name=' Jacky Cheung ', age=58, height=174}
3、 ... and 、lambda The ellipsis of expressions
The rules for using ellipsis are as follows :
1. Parameter types in parentheses can be omitted ;
2. If there is only one parameter in the lower bracket , Parentheses can also be omitted ;
3. If there is only one statement in braces , You can omit braces at the same time ,return Statements and semicolons
Before omitting :
(int a)->{
return new Person();
}
Omit :
a -> new Person()
Four 、lambda Preconditions for the use of expressions
lambda Expression syntax is very brief , but lambda Expressions are not misused , There are several conditions that need special attention when using :
1. The type of parameter or local variable of method must be interface type ;
2. There is only one abstract method in the interface ;
An interface with only one abstract method is called a functional interface , Just can use lambda expression ;
JDK8 I added a new one @FunctionalInterface annotation , It can help verify functional interfaces , If there are multiple abstract methods, an error will be reported .
边栏推荐
- Use of atomicinteger
- 2.6 preliminary cognition of synergetic couroutines
- Atomic atomic operation
- Adult adult adult
- Apprendre à concevoir des entités logicielles réutilisables à partir de la classe, de l'API et du cadre
- Solution à la défaillance de l'installation d'Electron
- DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
- [MySQL special] read lock and write lock
- Integer int compare size
- Unity3d learning notes 5 - create sub mesh
猜你喜欢
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
Swagger
Php Export word method (One MHT)
Summary of development issues
MCDF Experiment 1
Develop plug-ins for idea
Talk about the state management mechanism in Flink framework
ArcGIS application (XXI) ArcMap method of deleting layer specified features
ES6新特性
[MySQL special] read lock and write lock
随机推荐
Computer version wechat applet full screen display method, mobile phone horizontal screen method.
Qt OpenGL 纹理贴图
MySQL time zone solution
Swagger
Wrong arrangement (lottery, email)
Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
Kubernetes three dozen probes and probe mode
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
【附下载】密码获取工具LaZagne安装及使用
OPenGL 基本知识(根据自己理解整理)
Oracle advanced (I) realize DMP by expdp impdp command
SystemVerilog -- OOP -- copy of object
网络通讯之Socket-Tcp(一)
Laravel time zone timezone
During FTP login, the error "530 login incorrect.login failed" is reported
使用BLoC 构建 Flutter的页面实例
Introduction to concurrent programming (II)
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
Dart: about Libraries