当前位置:网站首页>Lambda 表达式
Lambda 表达式
2022-07-27 12:32:00 【涟涟涟涟】
为什么会有Lambda 表达式?
Java支持Lambda 表达式始于Java 8,它的出现简化了函数式接口匿名内部类的语法
怎么使用?
1.
其表达式语法如下:([参数1], [参数2], [参数3],… [参数n])->{代码块}
例:
看下面的main方法里的内容:
//匿名内部类:
@FunctionalInterface
interface IComputer {
void add(int a, int b);
}
public class Test {
public static void main(String[] args) {
IComputer computer = new IComputer() {
@Override
public void add(int a, int b) {
System.out.println(a + b);
}
};
computer.add(1, 1);
}
}
//Lambda 表达式:
@FunctionalInterface
interface IComputer {
void add(int a, int b);
}
public class Test {
public static void main(String[] args) {
IComputer computer = (int a, int b)-> {
System.out.println(a + b);
};
computer.add(1, 1);
}
}
2.
如果方法没有返回值且只有一行代码,则Lambda表达式语法可以是这种形式:([参数1], [参数2], [参数3],… [参数n])->单行语句,如下例:
//匿名内部类:
@FunctionalInterface
interface IMammal {
void move(String name);
}
public class Test {
public static void main(String[] args) {
IMammal whale = (name) -> {
System.out.println(name+"正在移动......");
};
whale.move("鲸鱼");
}
}
//Lambda 表达式:
@FunctionalInterface
interface IMammal {
void move(String name);
}
public class Test {
public static void main(String[] args) {
IMammal whale = (name) -> System.out.println(name+"正在移动......");
whale.move("鲸鱼");
}
}
3.
如果方法有返回值且只有一行代码,则Lambda表达式语法可以是这种形式:([参数1], [参数2], [参数3],… [参数n])->表达式,如下例:
//匿名内部类:
@FunctionalInterface
interface IComputer {
int add(int a, int b);
}
public class Test {
public static void main(String[] args) {
IComputer computer = (a, b) -> {
return a+b;
};
int result = computer.add(1,2);
System.out.println(result);
}
}
//Lambda 表达式:
@FunctionalInterface
interface IComputer {
int add(int a, int b);
}
public class Test {
public static void main(String[] args) {
IComputer computer = (a, b) -> a+b;
int result = computer.add(1,2);
System.out.println(result);
}
}
边栏推荐
- Alibaba cloud RDS exception during pool initialization
- HDU1698_Just a Hook
- Advance in the flutter project_ image_ Picker component usage
- 20210419 combined sum
- When the script runs in the background, it redirects the log from the console to its own named file
- 二分查找判定树(二分查找树平均查找长度)
- Makefile template
- Finally, I was ranked first in the content ranking in the professional field. I haven't been tired in vain during this period. Thanks to CSDN's official platform, I'm lucky and bitter.
- 12 pictures, take you to thoroughly understand ZGC garbage collector!
- Check the number of file descriptors opened by each process under the system
猜你喜欢

JVM memory layout detailed, illustrated, well written!

The strongest distributed locking tool: redisson

Chapter 10 enumeration classes and annotations

20210419 combined sum

Chapter 7 exception handling

HDU1698_ Just a Hook
One article to understand the index of like in MySQL

I do live e-commerce in tiktok, UK

XXL job parameter transfer

An overview of kernel compilation system
随机推荐
Vi. analysis of makefile.build
4. Analysis of the execution process of make modules for general purposes
Common usage of curl command
Top of the tide - reading notes + excerpts + insights
关于离线缓存Application Cache /使用 manifest文件缓存
How to use the server to build our blog
2021-3-17-byte-hu Pai
After Party A's hard work, 49.08 million orders of China Mobile were scrapped
Detailed explanation of flask framework
Finally, I was ranked first in the content ranking in the professional field. I haven't been tired in vain during this period. Thanks to CSDN's official platform, I'm lucky and bitter.
MySQL paging query instance_ MySQL paging query example explanation "suggestions collection"
Implicit indicators for evaluating the advantages and disadvantages of automated testing
Do you really understand the underlying data structure skip list of Zset in redis?
最强分布式锁工具:Redisson
Solution: can not issue executeupdate() or executelargeupdate() for selections
Go Introduction (2)
2021-3-22-tencent - minimum number of guards
Redistemplate cannot get the value according to the key
Openpyxl drawing area map
POJ1611_ The Suspects