当前位置:网站首页>Lamda expression
Lamda expression
2022-06-13 05:55:00 【A programmer in a wig】
A programmer in a wig Produce Tiktok ID: A programmer in a wig Welcome to your attention
Lamda expression
Basic concepts
1、 Avoid too many anonymous inner class definitions
2、 You can make your code look simple
3、 Removed a bunch of meaningless code , Leave the core logic
4、 Its essence belongs to the concept of functional programming
The common syntax is as follows :
(params)-> expression[ expression ]
(params) -> statement[ sentence ]
(params) -> {statements}
Case study :
new Thread(()-> System.out.println(Thread.currentThread().getName()),"Lamda Threads ").start();
application
Basic use
Functional interface :
stay jdk8 The functional interface is introduced in , An interface that has only one method is a functional interface .
such as :
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
public interface St{
public void testMethod();
}
- For functional interfaces , We can go through lamda Expression to create the object of the interface
Case study
/** * @author A programmer in a wig */
public class LamdaTest2 {
public static void main(String[] args) {
St st = null;
// Using anonymous inner classes to implement interfaces
st = new St() {
@Override
public void testMethod() {
System.out.println("st- Anonymous inner class ");
}
};
st.testMethod();
//Lamda Simplified operation
st = ()->{
System.out.println("Lamda Simplified operation ");
};
st.testMethod();
}
}
interface St{
public void testMethod();
}
In the case above ,st = ()->{…}
among () Represents the parameter list of methods in the interface .
{} Write the execution statement of the method in
therefore Lamda The way to simplify the operation is , Implementation interface St, And implementation methods testMethod, Method .
Of course, if there is only one execution statement in the method , We can also simplify : Omit {}
//lamda Simplified operation There is only one execution statement
st = ()->System.out.println(" There is only one sentence to execute Lamda simplify ");
st.testMethod();
Method implementation with parameters
In the example above , Methods in our interface have no parameters , So we can just write as follows :
()->{
statement;}
If there are parameters , You need to specify the parameters :
/** * @author A programmer in a wig */
public class LamdaTest2 {
public static void main(String[] args) {
St st = null;
//Lamda Simplified operation , stay () Specify the parameter type and name in
st = (int x)->{
System.out.println("Lamda Simplified operation :"+x);
};
st.testMethod(100);
//lamda Simplified operation , It can also be in () Write parameter name only
st = (x)->System.out.println(" There is only one sentence to execute Lamda simplify :"+x);
st.testMethod(1000);
// In the case of multiple parameters
Dk dk = null;
dk = (String a,int b)-> System.out.println(a+":"+b);
dk.dkMethod(" Zhang San ",15);
dk = (a,b)-> System.out.println(a+":"+b);
dk.dkMethod(" Li Si ",20);
}
}
interface St{
public void testMethod(int x);
}
interface Dk{
public void dkMethod(String a,int b);
}
Lamda Expression traversal set
Lamda Expressions can simplify Cllection and Map The traversal . Serve directly :
/** * @author A programmer in a wig */
public class LamdaTest3 {
public static void main(String[] args) {
// Get ready List
List<String> sts = new ArrayList<>();
sts.add(" Kakasi ");sts.add(" Sasuke ");sts.add(" Naruto ");
// Use Lamda Traverse , If only one sentence has the following form
sts.forEach((s)-> System.out.println(s+":Lamda"));
System.out.println("-----------------");
// Use Lamda Traverse , If there are multiple statement operations
sts.forEach((s)->{
if(s.length()<3)
System.out.println(s+":Lamda-2");
});
System.out.println("--------map----------");
// Get ready Map
Map<Integer,String> map = new HashMap<>();
map.put(1," Kakasi ");map.put(2," Sasuke ");map.put(3," Naruto ");
// Use Lamda Traverse , If only one sentence has the following form
map.forEach((k,v)-> System.out.println(k+":"+v+" Lamda"));
System.out.println("-----------------");
// Use Lamda Traverse , If there are multiple statement operations
map.forEach((k,v)-> {
if(v.length()<3)
System.out.println(k+":"+v+" Lamda-2");
});
}
}
边栏推荐
- Pychart professional edition's solution to SQL script error reporting
- Leetcode- minimum number of operations to make array elements equal - simple
- Misunderstanding of tongweb due to ease of use
- Explanation of sentinel series' features, composition and deployment
- The reason why the process cannot be shut down after a spark job is executed and the solution
- [China & some provinces and cities] JSON file for offline map visualization
- The SQL file of mysql8.0 was imported into version 5.5. There was a pit
- 计算两个时间相差的天数(支持跨月、跨年)
- Summary of the 11th week of sophomore year
- Zero copy technology
猜你喜欢

How to view tongweb logs correctly?
![[China & some provinces and cities] JSON file for offline map visualization](/img/ea/0c552e1e133f693b9902c54c2e09c8.jpg)
[China & some provinces and cities] JSON file for offline map visualization

Mongodb multi field aggregation group by

Concurrent programming -- what is threading?

NVIDIA Jetson nano/xavier NX capacity expansion tutorial

Building a stand-alone version of Nacos series

3. Postman easy to use

Etcd understanding of microservice architecture

How slow is the application system on tongweb? How dead is it?

Byte buddy print execution time and method link tracking
随机推荐
为什么那么多人讨厌A-Spice
顶部下滑沉浸式dialog
Shardingsphere JDBC exception: no table route info
2021.9.29 learning log MIME type
Mobile end adaptation scheme
20 flowable container (event sub process, things, sub process, pool and pool)
Leetcode Timo attack - simple
MySQL fuzzy query and sorting by matching degree
Sentinel series hot spot current limiting
Cross compile HelloWorld with cmake
One of PowerShell optimizations: prompt beautification
Byte buddy print execution time and method link tracking
2 first experience of drools
Leetcode- keyboard line - simple
Tongweb adapts to openrasp
Three paradigms of MySQL
2021.9.30 learning log -postman
19 calling subprocess (callactivity) of a flowable task
Leetcode- divide candy - simple
SPI primary key generation strategy for shardingsphere JDBC