当前位置:网站首页>Simple to use Lambda expressions
Simple to use Lambda expressions
2022-08-09 06:28:00 【hey hei hei hei】
Lambda表达式
语法
() -> {}
() are the parentheses of the interface method,如果有参数,也需要写参数.只有一个参数时,括号可以省略.
-> split left and right
{} 要实现的方法体,只有一行代码时,可以不加
概念
使用于lambdaThe interface for the expression is called函数型接口,A functional interface is simply an interface with abstract methods.
基本语法
interface Test1Service{
void sayMsg(String msg);
}
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test1Service ts = msg -> System.out.println("test"+msg);
ts.sayMsg("hello");
}
}
注意事项
- java8为函数式接口引入了一个新注解**@FunctionalInterface**,该注解主要用于编译级错误检查
- The interface contains two abstract methods,It violates the definition of functional interface,IDE会直接报错
- can be included in functional interfaces默认方法,因为默认方法不是抽象方法,其有一个默认实现,所以是符合函数式接口的定义的
- can be included in functional interfaces静态方法,因为静态方法不能是抽象方法,是一个已经实现了的方法,所以是符合函数式接口的定义的
- can be included in functional interfacesObject里的public方法,因为任何一个函数式接口的实现,默认都继承了Object类,包含了来自java.lang.Object里对这些抽象方法的实现
进阶
方法引用
We want to implement a method of an interface with another methodA类似,(这里的类似是指参数类型与返回值部分相同),我们直接声明A方法即可
There are three syntaxes for method references:
- 类::静态方法名
- 类::实例方法名
- 对象::实例方法名
interface Function{
String sendMsg(Object o);
@Override
boolean equals(Object obj);
}
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Function f = String::valueOf;
System.out.println(f.sendMsg(1));
System.out.println(f.sendMsg('c'));
}
}
构造引用
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
class Function3 {
private int x;
private String y;
Function3(){
x = 0;
y = "hello";
}
Function3(int x){
this.x = x;
y = "hello";
}
Function3(int x,String y){
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
@Override
public String toString() {
return "Function3 [x=" + x + ", y=" + y + "]";
}
}
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//无参
Supplier<Function3> f1 = () -> new Function3();
Function3 ft1 = f1.get();
System.out.println(f1.toString());
System.out.println(ft1);
//单参
Function<Integer, Function3> f2 = x -> new Function3(x);
System.out.println(f2.apply(1));
//双参
BiFunction<Integer, String, Function3> f3 = (x,y) -> new Function3(x,y);
System.out.println(f3.apply(1, "test"));
}
}
边栏推荐
- Qt 学习(三) —— Qt 模块
- BeautifulSoup4的介绍与使用
- 05 多线程与高并发 - ThreadPoolExecutor 源码解析
- 中英文说明书丨CalBioreagents 醛固酮单克隆抗体
- 为什么以太网无法接收大于1500字节的数据包?
- Superparamagnetic iron [email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop
- Xilinx Zynq ZynqMP DNA
- Output method of list string print(*a) print(““.join(str(c) for c in a) )
- 如何操作数据库
- Web APIs BOM- 操作浏览器:本地存储
猜你喜欢
中英文说明书丨CalBioreagents ACTH N端单克隆抗体
工控设备的系统如何进行加固
mongo+ycsb性能测试及线程数分析
带头双向循环链表的增删查改(C语言实现)
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
static静态关键字和继承
How to find package information and pin definitions for NXP S32K1xx series microcontrollers
ZIP压缩包文件删除密码的方法
一道很简答但是没答对的SQL题
install flask
随机推荐
中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
Can Jincang Database Set Transaction Automatic Commit?
中英文说明书丨CalBioreagents ACTH N端单克隆抗体
像天才一样思考:如何培养自己的创造力?
2022.8.8DAY628
Excel受保护的工作表怎么操作?
力扣刷题180
PDF不能打印和复制的问题如何解决?
Remember a nest.js route that matches all the path problems that follow
6 states of a thread
install flask
为什么以太网无法接收大于1500字节的数据包?
MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
线程的6种状态
直接用的zip包 缺少很多依赖,pip没有,感觉用anaconda create一个环境会方便点
BeautifulSoup4的介绍与使用
运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
使用百度EasyDL实现智能垃圾箱
How to find package information and pin definitions for NXP S32K1xx series microcontrollers
Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)