java8学习教程之lambda表达式的使用方法
前言
我们在上一篇文章中介绍了lambda表达式的语法,引入了lambda表达式的使用场景,以及使用lambda表达式的好处。我们将在这篇文章中,已实例讲解如何定义和使用lambda表达式,以及与其它语言相比,lambda表达式在Java中的特殊规范。
使用匿名内部类的例子
首先明确一点,在Java8出现之前,lambda表达式能够做到的,使用内部类也能做到,lambda表达式只是简化了编程。
下面的例子是从列表中根据条件挑选出读者。
定义TantanitReader:
publicclassTantanitReader{
privateintage;
privateStringloginName;
privateStringrealName;
privateStringcareer;
publicTantanitReader(){
}
publicTantanitReader(intage,StringloginName,StringrealName,Stringcareer){
this.age=age;
this.loginName=loginName;
this.realName=realName;
this.career=career;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetLoginName(){
returnloginName;
}
publicvoidsetLoginName(StringloginName){
this.loginName=loginName;
}
publicStringgetRealName(){
returnrealName;
}
publicvoidsetRealName(StringrealName){
this.realName=realName;
}
publicStringgetCareer(){
returncareer;
}
publicvoidsetCareer(Stringcareer){
this.career=career;
}
@Override
publicStringtoString(){
return"age:"+this.getAge()+",loginName:"+this.loginName
+",realName:"+this.getRealName()+",career:"+this.getCareer();
}
}
定义判断的接口:
publicinterfacePredicate{ booleantest(Tt); }
定义选择函数:
publicclassSelectService{ publicList select(Collection source,Predicate predicate){ Listresult=newLinkedList(); for(Telement:source){ if(predicate.test(element)){ result.add(element); } } returnresult; } }
编写测试用的例子,分别选择成年读者和十多岁(包括10岁)的读者:
publicclassTantanitReaderPredicateTest{
publicstaticvoidmain(String[]args){
SelectServicetantanitReaderSelectSerive
=newSelectService();
Listsource=newLinkedList<>();
source.add(newTantanitReader(10,"jack","张三","学生"));
source.add(newTantanitReader(18,"rose","李四","学生"));
source.add(newTantanitReader(19,"mike","王五","程序员"));
source.add(newTantanitReader(20,"jack","赵六","作家"));
ListaudultReaders
=tantanitReaderSelectSerive.select(source,newPredicate(){
@Override
publicbooleantest(Objecto){
TantanitReadertantanitReader=(TantanitReader)o;
returntantanitReader.getAge()>=18;
}
});
System.out.println("tantanit.com成年读者名单如下:");
printTantanitReaders(audultReaders);
System.out.println("tantanit.com十多岁(包含10岁)成员如下:");
ListteenReaders
=tantanitReaderSelectSerive.select(source,newPredicate(){
@Override
publicbooleantest(Objecto){
TantanitReadertantanitReader=(TantanitReader)o;
returntantanitReader.getAge()>=10&&tantanitReader.getAge()<=19;
}
});
printTantanitReaders(teenReaders);
}
publicstaticvoidprintTantanitReaders(ListtantanitReaders){
for(TantanitReadertantanitReader:tantanitReaders){
System.out.println(tantanitReader.toString());
}
}
}
执行后,打印结果如下:
tantanit.com成员读者名单如下: age:18,loginName:rose,realName:李四,career:学生 age:19,loginName:mike,realName:王五,career:程序员 age:20,loginName:jack,realName:赵六,career:作家 tantanit.com十多岁(包含10岁)成员如下: age:10,loginName:jack,realName:张三,career:学生 age:18,loginName:rose,realName:李四,career:学生 age:19,loginName:mike,realName:王五,career:程序员
可以看到,两次选择读者,都需要newPredicate(),并且重写(Override)test方法,而真正的差异其实只在于判断语句:
tantanitReader.getAge()>=18
和
tantanitReader.getAge()>=10&&tantanitReader.getAge()<=19
但是在Java8之前,由于没有lambda表达式,只能忍受这种冗余。如何用lambda表达式来简化代码呢?
为了照顾Java开发人员既有的编程习惯,与其它语言不同,Java8在设计lambda表达式的使用机制时,规定仍然需要使用接口,并且要求所使用的接口必须是函数式接口,在这个例子中,我们仍然可以使用:
publicinterfacePredicate{ booleantest(Tt); }
因为这个接口只有一个抽象方法(java8引入了default方法,default方法有具体实现,不算抽象方法),所以它是函数式接口(functionalinterface)。函数式接口可以加上@FunctionalInterface声明,也可以不加。但是加上之后,编译器在编译阶段就会检查这个接口是否符合函数式接口的定义,所以这里我们定义一个新的接口,并且加上@FunctionalInterface声明:
@FunctionalInterface publicinterfacePredicateFunction{ booleantest(Tt); }
并且给SelectService添加一个以PredicateFunction为参数的方法:
publicListselect(Collection source,PredicateFunction predicate){ Listresult=newLinkedList(); for(Telement:source){ if(predicate.test(element)){ result.add(element); } } returnresult; }
再修改测试的例子:
publicclassTantanitReaderPredicateFunctionTest{
publicstaticvoidmain(String[]args){
SelectServicetantanitReaderSelectSerive
=newSelectService();
Listsource=newLinkedList<>();
source.add(newTantanitReader(10,"jack","张三","学生"));
source.add(newTantanitReader(18,"rose","李四","学生"));
source.add(newTantanitReader(19,"mike","王五","程序员"));
source.add(newTantanitReader(20,"jack","赵六","作家"));
PredicateFunctionpredicateFunction
=(TantanitReadertantanitReader)->tantanitReader.getAge()>=18;
ListaudultReaders
=tantanitReaderSelectSerive.select(source,predicateFunction);
System.out.println("tantanit.com成员读者名单如下:");
printTantanitReaders(audultReaders);
System.out.println("tantanit.com十多岁(包含10岁)成员如下:");
PredicateFunctionpredicateFunction2
=(TantanitReadertantanitReader)
->tantanitReader.getAge()>=10&&tantanitReader.getAge()<=19;
ListteenReaders
=tantanitReaderSelectSerive.select(source,predicateFunction2);
printTantanitReaders(teenReaders);
}
publicstaticvoidprintTantanitReaders(ListtantanitReaders){
for(TantanitReadertantanitReader:tantanitReaders){
System.out.println(tantanitReader.toString());
}
}
}
下面我们分析一下这段代码是如何生效的:
PredicateFunctionpredicateFunction =(TantanitReadertantanitReader)->tantanitReader.getAge()>=18; List audultReaders =tantanitReaderSelectSerive.select(source,predicateFunction);
这段代码,生成了一个PredicateFunction类型的实例,并且将该实例的引用作为参数传给tantanitReaderSelectSerive的select方法,并且执行select方法。select在执行过程中,调用predicateFunction的test方法,而test方法的内容就是我们传入的lambda表达式,最终按照lambda表达式,选择出读者。
再进一步,一般可以不定义predicateFunction这个变量,而直接将lambda表达式作为参数传给tantanitReaderSelectSerive的select方法,像这样:
ListaudultReaders =tantanitReaderSelectSerive.select( source,(TantanitReadertantanitReader)->tantanitReader.getAge()>=18 );
但是这个例子,实际上会报编译错误,说TantanitReader和tantanitReaderSelectSerive的select方法的定义不匹配,因为select方法使用的是泛型。java8的文档确实是规定了在使用泛型的情况下,不能直接将lambda表达式作为参数,这个挺无语的。如果不使用泛型的,没有这个问题。
小结
下面总结一下如何使用lambda表达式
- 首先,定义一个函数式接口(functionalinterface),并且在接口中定义需要使用的抽象方法。
- 编写业务方法,并且以该函数式接口作为参数,并且调用该接口定义的方法,完成业务逻辑。
- 调用业务方法,并且将lambda表达式作为参数传入。
如果使用了泛型,最后一步改为先定义一个函数式接口的实例的引用,再作为参数传给业务方法。
此外,lambda表达式还可以继续简化为函数引用,将在后面的文章中讲解。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。