当前位置:网站首页>Lambda expression
Lambda expression
2022-07-27 12:39:00 【Rippling rippling】
Why is there Lambda expression ?
Java Support Lambda Expression starts with Java 8, Its appearance simplifies the syntax of anonymous inner classes of functional interfaces
How do you use it? ?
1.
The expression syntax is as follows :([ Parameters 1], [ Parameters 2], [ Parameters 3],… [ Parameters n])->{ Code block }
example :
Look at the following main The content of the method :
// Anonymous inner class :
@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 expression :
@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.
If the method has no return value and only one line of code , be Lambda Expression syntax can be in this form :([ Parameters 1], [ Parameters 2], [ Parameters 3],… [ Parameters n])-> Single line statement , Here's an example :
// Anonymous inner class :
@FunctionalInterface
interface IMammal {
void move(String name);
}
public class Test {
public static void main(String[] args) {
IMammal whale = (name) -> {
System.out.println(name+" Moving ......");
};
whale.move(" Whale ");
}
}
//Lambda expression :
@FunctionalInterface
interface IMammal {
void move(String name);
}
public class Test {
public static void main(String[] args) {
IMammal whale = (name) -> System.out.println(name+" Moving ......");
whale.move(" Whale ");
}
}
3.
If the method has a return value and only one line of code , be Lambda Expression syntax can be in this form :([ Parameters 1], [ Parameters 2], [ Parameters 3],… [ Parameters n])-> expression , Here's an example :
// Anonymous inner class :
@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 expression :
@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);
}
}
边栏推荐
- 详述try-catch-finally
- The strongest distributed locking tool: redisson
- 20210518-Cuda
- Photoshop web design tutorial
- Plus版SBOM:流水线物料清单PBOM
- V. introduction of other objectives and general options
- 概述静态内部类与非静态内部类
- How to use the server to build our blog
- Implicit indicators for evaluating the advantages and disadvantages of automated testing
- I/O实例操作
猜你喜欢

20210518-Cuda

CEPH distributed storage performance tuning (6)

MySQL扩展

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.

最强分布式锁工具:Redisson

js真伪数组转换

How to ask questions on the road for the first time - necessary skills for self-study (with live playback)

隔离级别

The song of the virtual idol was originally generated in this way!

(10) STM32 - systick tick timer
随机推荐
BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)
SSM实战项目-前后分离(简单易懂)
(10) STM32 - systick tick timer
redis分布式在线安装
2021-03-15
如何获取Class类对象
虚拟偶像的歌声原来是这样生成的!
最强分布式锁工具:Redisson
The song of the virtual idol was originally generated in this way!
Go Beginner (4)
Use redis' sorted set to make weekly hot reviews
Interviewer: how to deal with the data loss of redis master-slave cluster switching?
MySQL扩展
2021-3-22-tencent - minimum number of guards
[database data recovery] a data recovery case in which the disk partition where the SQL Server database is located is insufficient and an error is reported
Plus SBOM: assembly line BOM pbom
2021-3-22-directed graph sorting
关于 CMS 垃圾回收器,你真的懂了吗?
【数据库数据恢复】SQL Server数据库所在磁盘分区空间不足报错的数据恢复案例
Go Beginner (3)