目录
Lambda表达式背景引入
Lambda是Java8的新特性,使用Lambda表达式会使设计的代码更简洁,用它可以替代我们以前写的匿名内部类来实现接口,Lambda表达式本质上是一个匿名函数。
下面我们通过一个简单的例子来体会一下Lambda表达式:
首先我们定义一个Compute接口
public interface Compute {
int add(int a,int b);
}
接着我们下ComputeTest的main方法中实现这个接口并调用接口里面的函数
public class ComputeTest {
public static void main(String[] args) {
Compute compute=new Compute() {
@Override
public int add(int a, int b) {
return a+b;
}
};
int sum=compute.add(1,2);
System.out.println(sum);
}
}
以上就是我们以前实现匿名内部类并调用方法的步骤,可见代码量繁琐。
下面我们采用Lambda表达式
public class ComputeLambda {
public static void main(String[] args) {
Compute compute=(int a,int b)->{return a+b;};
System.out.println(compute.add(1,2));
}
对于匿名内部类的实现直接改写成Compute compute=(int a,int b)->{return a+b;}
这样写的话,简洁多了,是不是感觉Lambda表达式挺强大的,接下来我们来探究一下Lambda表达式的详细语法。
Lambda表达式语法
我们看下这个Lambda表达式:
(int a,int b) ->{return a+b;};
这个本质是一个函数;
一般的函数类似如下:
int add(int a,int b){
return a+b;
}
有返回值,方法名,参数列表,方法体
Lambda表达式函数的话,只有参数列表,和方法体;
( 参数列表 ) -> { 方法体 }
说明:
( ) :用来描述参数列表;
{ } : 用来描述方法体;
-> :Lambda运算符,可以叫做箭头符号
Lambda表达式语法详讲
写六个接口,包含有参数无参数,有返回值无返回值的情况
interface If1{
//无参数无返回值接口
void test();
}
interface If2{
//有返回值无参数
int test();
}
interface If3{
//单个参数有返回值
int test(int a);
}
interface If4{
//单个参数无返回值
void test(int a);
}
interface If5{
//多个参数无返回值
void test(int a,int b);
}
interface If6{
//多个参数有返回值
int test(int a,int b);
}
下面在主方法中利用Lambda表达式实现这些接口并调用它
public interface LambdaTest {
public static void main(String[] args) {
//情况一:无参数无返回值
If1 if1=()->{
System.out.println("无参数,无返回值");
};
if1.test();
//情况二:无参数有返回值
If2 if2=()->{
System.out.println("无参数,有返回值");
return 100;
};
System.out.println(if2.test());
//情况三:单个参数有返回值
If3 if3=(int a)->{
System.out.println("单个参数有返回值");
return a+1;
};
System.out.println(if3.test(1));
//情况四:单个参数无返回值
If4 if4=(int a)->{
System.out.println("单个参数无返回值,参数="+a);
};
if4.test(3);
//情况五:多个参数无返回值
If5 if5=(int a,int b)->{
System.out.println("多个参数无返回值,参数为a="+a+"b="+b);
};
if5.test(1,2);
//情况六:多个参数有返回值
If6 if6=(int a,int b)->{
System.out.println("多个参数有返回值");
return a+b;
};
System.out.println(if6.test(34,34));
}
}
运行结果如下:
Lambda表达式的精简语法
精简语法注意点:
1,参数类型可以省略
2,假如只有一个参数,()括号可以省略
3,如果方法体只有一条语句,{}大括号可以省略
4,如果方法体中唯一的语句是return返回语句,那省略大括号的同时return也要省略
下面我秉持精简语法的规则重写一下上面的语句
public interface LambdaTest {
public static void main(String[] args) {
//情况一:无参数无返回值
//大括号内只有一条语句,省略大括号
If1 if1=()->System.out.println("无参数,无返回值");
if1.test();
//情况二:无参数有返回值
If2 if2=()->{
System.out.println("无参数,有返回值");
return 100;
};
System.out.println(if2.test());
//情况三:单个参数有返回值
//省略参数类型,只有一个参数,省略参数括号
If3 if3=a->{
System.out.println("单个参数有返回值");
return a+1;
};
System.out.println(if3.test(1));
//情况四:单个参数无返回值
//省略参数类型,只有一个参数,省略括号,大括号只有一条语句省略大括号
If4 if4= a->System.out.println("单个参数无返回值,参数="+a);
if4.test(3);
//情况五:多个参数无返回值
//省略参数类型,大括号只有一条语句省略大括号
If5 if5=(a,b)->System.out.println("多个参数无返回值,参数为a="+a+"b="+b);
if5.test(1,2);
//情况六:多个参数有返回值
//省略参数类型
If6 if6=(a,b)->{
System.out.println("多个参数有返回值");
return a+b;
};
System.out.println(if6.test(34,34));
}
}
方法引用
有时候多个lambda表达式实现函数是一样的话,我们可以封装成通用方法,以便于维护;
这时候可以用方法引用实现:
语法是:对象::方法
假如是static方法,可以直接 类名::方法
public class LambdaTest {
public static void main(String[] args) {
LambdaTest lambdaTest=new LambdaTest();
If6 if6=lambdaTest::test;
If6 if61=LambdaTest::test1;
System.out.println(if6.test(1,2));
System.out.println(if61.test(2,4));
}
//定义这两个方法
public int test(int a, int b){
return a+b;
}
public static int test1(int a,int b){
return a+b;
}
}
interface If6{
//多个参数有返回值
int test(int a,int b);
}
构造方法引用
如果函数式接口的实现恰好可以通过调用一个类的构造方法来实现,
那么就可以使用构造方法引用;
语法:类名::new
public class Person {
String name;
Integer age;
public Person(){
System.out.println("无参数构造方法");
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
System.out.println("有参数构造方法");
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class LambdaDemo {
public static void main(String[] args) {
//普通方式
PersonService personService=()->{
return new Person();
};
personService.getPerson();
//简化方式
PersonService personService_1=()->new Person();
personService_1.getPerson();
//构造方法引用
PersonService personService1_2=Person::new;
personService1_2.getPerson();
PersonService1 personService1=Person::new;
personService1.getPerson("张三",18);
}
}
interface PersonService{
Person getPerson();
}
interface PersonService1{
Person getPerson(String name,Integer age);
}
执行结果:
无参数构造方法
无参数构造方法
无参数构造方法
有参数构造方法
综合案例
下面我们通过一个lambda操作集合的综合实例,来深入体验下Lambda表达式用法
public class PersonLambdaDemo {
public static void main(String[] args) {
List<Person>list=new ArrayList<>();
list.add(new Person("张三",28));
list.add(new Person("李四",19));
list.add(new Person("王五",30));
list.add(new Person("赵六",21));
System.out.println("Lambda集合排序");
list.sort((o1,o2)->o1.getAge()-o2.getAge());
System.out.println(list);
System.out.println("Lambda遍历集合");
list.forEach(System.out::println);
}
}
运行结果:
Lambda集合排序
[Person{name='李四', age=19}, Person{name='赵六', age=21}, Person{name='张三', age=28}, Person{name='王五', age=30}]
Lambda遍历集合
Person{name='李四', age=19}
Person{name='赵六', age=21}
Person{name='张三', age=28}
Person{name='王五', age=30}
我们来分析下集合的sort方法
sort方法里有一个Comparator接口,再点进去看下
我们通过lambda就可以轻松实现排序:
(o1,o2)->o1.getAge()-o2.getAge()
再看下集合的forEach方法,点进去
有个消费者Consumer接口,再点进去:
接口里有个接口参数的accept的方法;
所以我们直接方法引用 直接输出每次的遍历值即可;
System.out::println
@FunctionalInterface注解
前面我们会发现Consumer接口,Comparator接口都有
@FunctionalInterface注解;
这个注解是函数式接口注解,所谓的函数式接口,当然首先是一个接口,然后就是在这个接口里面只能有一个抽象方法。
这种类型的接口也称为SAM接口,即Single Abstract Method interfaces
特点
-
接口有且仅有一个抽象方法
-
允许定义静态方法
-
允许定义默认方法
-
允许java.lang.Object中的public方法
-
该注解不是必须的,如果一个接口符合"函数式接口"定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了@FunctionInterface,那么编译器会报错
// 正确的函数式接口 @FunctionalInterface public interface TestInterface { // 抽象方法 public void sub(); // java.lang.Object中的public方法 public boolean equals(Object var1); // 默认方法 public default void defaultMethod(){ } // 静态方法 public static void staticMethod(){ } } // 错误的函数式接口(有多个抽象方法) @FunctionalInterface public interface TestInterface2 { void add(); void sub(); }
系统内置函数式接口
Java8的推出,是以Lambda重要特性,一起推出的,其中系统内置了一系列函数式接口;
再jdk的java.util.function包下,有一系列的内置函数式接口:
比如常用的Consumer,Comparator,Predicate,Supplier等;