lambda 表达式的基础语法: Java 8 中引入了操作符 -> 箭头操作 lambda 操作符
箭头操作符就lambda表达式拆分为两部分:
左侧:lambda 表达式的参数列表
右侧:lambda 表达式所需要执行的功能, lambda体
语法格式1: 无参数,无返回值
Runnabler=()->{
System.out.println("hello java");
};
语法格式2 : 一个参数,无返回值
//对抽象方法的实现 接口只有一个方法
Consumer<String>con=(x)->System.out.println(x);
con.accept("hello java");
语法格式3 : 有一个参数,,小括号可以不写
语法格式4 : 有两个及以上的参数,并且lambda体中有多条语句,并且有返回值
Comparator<Integer>com=(x,y)->{
System.out.println("hello Java");
returnInteger.compare(x,y);
};
5 : 若lambda体中只有一条语句, return 和大括号都可以不写
Comparator<Integer>com=(x,y)->Integer.compare(x,y);
6 : lambda 表达式的参数列表的数据类型可以不写,jvm编译器可以通过上下文推断出,数据类型, 类型推断
二 Lambda 表达式需要函数式接口的支持
函数式接口: 接口中只有一个抽象方法的接口,称为函数式接口,可以使用注解 @FunctionalInterface 修饰 可以检查是否是函数式接口
java 8 中内置的四大核心函数式接口
Consumer<T> : 消费性接口*
void accept(T t)
Supplier<T> : 供给型接口
T get();
Function<T, R> : 函数型接口
R apply(T t)
Predicate<T> : 断言型接口*
boolean test(T t)
//consumer 消费性接口@Test
publicvoidtest1() {
happy(10000, (x)->{
System.out.println("消费了"+x+"元");
});
}
publicvoidhappy(doublemoney,Consumer<Double>con) {
con.accept(money);
}
//supplier<T> 供给型接口
@Test
publicvoidtest2() {
List<Integer>numbers=getNumbers(10, ()->(int)(Math.random()*100));System.out.println(numbers);
}//产生一些整数,并放入集合中
publicList<Integer>getNumbers(intnum,Supplier<Integer>sup) {ArrayList<Integer>list=newArrayList<>();
for(inti=0;i<num;i++) {
Integern=sup.get();
list.add(n);
}
returnlist;
}
//Function<T, R> : 函数型接口
//需求:用于处理字符串
publicStringstrHandler(Stringstr,Function<String,String>fun) {
returnfun.apply(str);
}
@Test
publicvoidtest3() {
StringnewStr=strHandler("/t/t/t hallo Java ", (str)->str.trim());
System.out.println(newStr);
}
方法引用
方法引用 : 若lambda 体中的内容有方法已经实现了,我们可以使用方法引用
* (可以理解为方法引用是lambda 表达式的另外一种表现形式)
* 主要有三种语法格式
* 对象::实例方法名
* 类::静态方法名
* 类::实例方法名
* 注意:Lambda 体中调用方法的参数列表与返回值类型,要与函数式接口中抽象方法的函数列表和返回值类型保持一致。
* 若lambda 参数列表中的第一个参数是实例方法的调用者,第二个参数是实例方法的参数时,乐意使用CLassName::method
//对象::实例方法名
@Test
publicvoidtest1() {
Consumer<String>con=(x)->System.out.println(x);
PrintStreamps=System.out;
Consumer<String>con1=ps::println;
con1.accept("acvd");
}
@Test
publicvoidtest2() {
Employeeemp=newEmployee(111,"zs",12,1000);
Supplier<String>sup=()->emp.getName();
Stringstr=sup.get();
Supplier<Integer>sup2=emp::getAge;
Integerinteger=sup2.get();
System.out.println(integer);
}
//类::静态方法名
@Test
publicvoidtest3() {
Comparator<Integer>com=(x,y)->Integer.compare(x,y);
Comparator<Integer>com1=Integer::compare;
}
//类::实例方法名
@Test
publicvoidtest4() {
BiPredicate<String,String>bp=(x,y)->x.equals(y);
BiPredicate<String,String>bp2=String::equals;
}
二 : 构造器引用:
*
* 格式:
*
* ClassName::new
*
* 注意: 需要调用的构造器的参数列表与函数接口中抽象方法的参数列表保持一致
//构造器引用
@Test
publicvoidtest5() {
Supplier<Employee>sup=()->newEmployee();
//构造器引用方式 午餐
Supplier<Employee>sup2=Employee::new;
Employeeemployee=sup2.get();
System.out.println(employee);
}
注意: 需要调用的构造器的参数列表与函数接口中抽象方法的参数列表保持一致
*
* 三 数组引用
*
* Type :: new
*
//数组引用
@Test
publicvoidtest6() {
Function<Integer,String[]>fun=(x)->newString[x];
String[]apply=fun.apply(10);
System.out.println(apply.length);
Function<Integer,String[]>fun2=String[]::new;
String[]apply1=fun2.apply(20);
System.out.println(apply1.length);
}