当前位置:网站首页>函数式接口&Lambda表达式——简单应用笔记
函数式接口&Lambda表达式——简单应用笔记
2022-07-30 08:21:00 【Rose*留白ق೨】
以下是根据资料和实验得出的理解:
1、Lambda表达式的使用与函数式接口有关
2、函数式接口有且仅有一个抽象方法
函数式接口、Lambda表达式不懂的可以去参考其他文章
以下代码可以自行复制粘贴看看执行效果
//函数式接口 接口内仅有一个方法
@FunctionalInterface
public interface MyFunctionalInterface {
int test(int a,int b);
}
import java.util.ArrayList;
import java.util.List;
public class testLambda {
private static int sum(MyFunctionalInterface myFunctionalInterface,int a,int b){
return myFunctionalInterface.test(a,b);
}
public static void main(String[] args) {
//Lambda表达式经典例子
List<String> list = new ArrayList<>();
list.add("abc");
list.add("def");
list.add("ghi");
list.forEach(item -> System.out.println(item));
//函数式接口的基本使用 基本语法: (parameters) -> expression 或 (parameters) ->{ statements; }
MyFunctionalInterface myFunctionalInterface = (int a,int b)-> {
return a+b;
};
//简写
MyFunctionalInterface myFunctionalInterface1 = (int a,int b)-> a+b;
int result = myFunctionalInterface1.test(3,2);
System.out.println(result);
//函数式接口的典型使用场景是作为方法的参数
int result1 = sum((int a,int b) -> a+b,20,36);
System.out.println(result1);
}
}
边栏推荐
- Thinking about digital transformation of construction enterprises in 2022, the road to digital transformation of construction enterprises
- JS中如何阻止事件冒泡和默认行为
- EMC过不了?都是PCB工程师的锅?
- Is R&D moving to FAE (Field Application Engineer), is it moving away from technology?Is there a future?
- 积分简明笔记-第一类曲线积分的类型
- 剖析SGI STL空间配置器(一 、辅助接口函数)
- [Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
- 统一异常处理导致ResponseBodyAdvice失效
- How to use Jmeter to carry out high concurrency in scenarios such as panic buying and seckill?
- Kubernetes 在科技革命中的演变
猜你喜欢
硬件工程师
PCB板加工流程中哪些因素会影响到传输线阻抗
It is said that FPGA is high-end, what can it do?
剖析SGI STL空间配置器(_S_refill内存块填充函数)
How to Assemble a Registry
Thinking about digital transformation of construction enterprises in 2022, the road to digital transformation of construction enterprises
20个电路能懂5个以上,足以证明你在电子行业混过!
英语语法-名词性从句
激活数据潜力 亚马逊云科技重塑云上存储“全家桶”
【无标题】
随机推荐
Reflection tricks can boost your performance by N times
【WeChat Mini Program】Page Events
C语言经典练习题(3)——“汉诺塔(Hanoi)“
电源完整性的去耦和层间耦合电容
Unity性能分析 Unity Profile性能分析工具
opencv可以有多有趣
2022杭电多校第一场
电路分析:运放和三极管组成的恒流源电路
[Fun BLDC series with zero basics] Taking GD32F30x as an example, the timer related functions are explained in detail
积分专题笔记-与路径无关条件
电源完整性基础知识
桌面软件开发框架大赏
JS中如何阻止事件冒泡和默认行为
ant-design form表单校验upload组件(附个人封装的上传组件)
TreeSet解析
SQL window function
经典毕业设计:基于SSM实现高校后勤报修系统
BaseQuickAdapter方法getBindingAdapterPosition
【无标题】
Concise Notes on Integrals - Types of Curve Integrals of the Second Kind