1-Lambda表达式
Lambda是一个匿名函数,我们可以把Lambda表达式理解为一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。
//原来的匿名内部类
@Test
public void test1(){
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1,o2);
}
};
TreeSet<Integer> ts = new TreeSet<>(comparator);
}
//Lambda表达式
@Test
public void test2(){
Comparator<Integer> comparator = (x,y)->Integer.compare(x,y);
TreeSet<Integer> ts = new TreeSet<>(comparator);
}
Lambda表达式在Java语言中引入了"->"操作符,该操作符被称为Lambda操作符。它将Lambda分为两部分:
左侧:指定了Lambda表达式所需要的所有参数
右侧:指定了Lambda体,即Lambda表达式要执行的功能
Lambda表达式语法
语法一:无参,无返回值,Lambda体只需一条语句
Runnable run = ()->System.out.println("Hello Lambda!");
语法二:Lambda需要一个参数
Consumer<String> fun = (args)-> System.out.println(args);
语法三:Lambda需要一个参数是,参数的小括号可以省略
Consumer<String> fun = args-> System.out.println(args);
语法四:Lambda需要两个参数,并且有返回值
BinaryOperator<Long> bo = (x,y)->{
System.out.println("实现函数接口方法");
return x+y;
}
语法五:当Lambda只有一条语句时,return与大括号可以不写
BinaryOperator<Long> bo = (x,y)-> return x+y;
语法六:数据类型可以省略,因为可以由编译器推断得出,称为“类型推断” ,这是因为javac根据程序的上下文,在后台推断出参数的类型。
BinaryOperator<Long> bo = (Long x,Long y)->{
System.out.println("实现函数接口方法");
return x+y;
}
函数式接口
- 只包含一个抽象方法的接口,称为函数式接口
- 你可通过Lambda表达式来创建该接口的对象。
- 我们可以在任意函数式接口上使用@FunctionalInterface注解,这样做可以检查它是否是一个函数式接口,同时,javadoc也会包含一条声明,说明这个接口是一个函数式接口
@FunctionalInterface
public interface MyFun<T> {
public T getValue(T num);
}
public String toUpperString(MyFun<String> mf,String str){
return mf.getValue(str);
}
String newStr = toUpperString(str->str.toUpperCase(),"abcdef");
Java内置四大核心函数式接口

方法引用
若Lambda体中的内容有已经实现的方法,我们可以使用”方法引用“(可以理解为方法引用是Lambda表达式的另外一种表现形式)
主要的格式有三种:
- 对象::实例方法名
- 类::静态方法名
- 类::实例方法名
Lambda体中调用方法的参数列表和返回值类型,要与函数式接口中抽象方法的函数列表和返回类型保持一致
若Lambda参数列表中第一个参数是实例方法的调用者,而第二个参数是实例方法的参数时,可以使用ClassName::method
构造器引用
格式:
ClassName::new
数组引用
格式
Type[]::new
//对象::实例方法
@Test
public void test1(){
Consumer<String> con = (x)-> System.out.println(x);
PrintStream ps = System.out;
Consumer<String> consumer = ps::println;
}
@Test
public void test2(){
Employee employee = new Employee();
Supplier<String> sup = ()->employee.getName();
String str = sup.get();
System.out.println(str);
Supplier<Integer> sup1 = employee::getAge;
Integer integer = sup1.get();
System.out.println(integer);
}
//类::静态方法名
public void test3(){
Comparator<Integer> com = (x,y)->Integer.compare(x,y);
Comparator<Integer> com2 = Integer::compare;
}
//类::实例方法
@Test
public void test4(){
BiPredicate<String,String> bp = (x,y)->x.equals(y);
BiPredicate<String,String> bp2 = String::equals;
}
//构造器引用
@Test
public void test5(){
Supplier<Employee> supplier = ()->new Employee();
supplier.get();
Supplier<Employee> supplier2 = Employee::new;//无参构造器
Employee emp = supplier2.get();
System.out.println(emp);
}
@Test
public void test6(){
Function<Integer,Employee> fun = (x)->new Employee(x);
Function<Integer,Employee> fun2 = Employee::new;
Employee employee = fun2.apply(35);
System.out.println(employee);
}
//数组引用:
@Test
public void test7(){
Function<Integer,String[]> fun = (x)->new String[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);
}
本文深入探讨Java中的Lambda表达式,介绍其语法、如何简化代码,以及与函数式接口的关系。同时,文章还讲解了方法引用、构造器引用和数组引用的概念及应用。

被折叠的 条评论
为什么被折叠?



