文章目录
Lambda表达式引入
假设我们现在有一个遍历集合List的需求
a.首先我们通过Lambda表达式的写法实现:
代码如下(示例):
public class MyLambdaTest {
public static void main(String[] args) {
List<String> stringList = Arrays.asList("肌肉猿","肌肉猿爱编程","程序员非晚");
stringList.forEach(s -> {
System.out.println(s);
});
}
程序运行结果:
b.我们通过匿名内部类的形式实现
public class MyLambdaTest2 {
public static void main(String[] args) {
List<String> stringList = Arrays.asList("肌肉猿","肌肉猿爱编程","程序员非晚");
// 通过匿名内部类的形式替代Lambda表达式
stringList.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
}
}
程序运行结果:
上述代码分析:
foreach()方法是Iterable接口的一个默认方法,在下面的方法的参数列表中我们可以知道,该方法需要一个Consumer类型的参数,方法体的内容则是一个for循环,进行对每一个对象的便利,最终处理方法则是调用accept()方法。
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
当我们继续查看Consumer的accept(T)方法,我们不难得出Consumer是一个函数式接口(该接口的详细讲解见我专栏里的文章有详解)。
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
此时此刻,我们通过对比上述的两种实现遍历集合List方式不难得出,stringList.forEach(s -> {System.out.println(s);})。
Lambda表达式s -> {System.out.println(s);}其实本质上就是实现了Consumer接口的一个匿名(内部类)对象。
大括号里的内容(System.out.println(s))相当于重写了accept()方法。
具体内部的底层实现细节见我专栏的系列文章
一、Lambda表达式的方法引用
经过目前的分析,使用lambda表达式就是传递进去的代码就是一种解决方案,拿什么参数,做什么操作,但是在使用的时候要注意冗余的问题出现。
a.冗余Lambda场景实例
首先我们编写简单接口来应用Lambda表达式
@FunctionalInterface
public interface t1 {
void print(String str);
}
接下来我们用Lambda实现上述接口中的print打印方法
public class UseT1Test1 {
private static void printString(t1 data){
data.print("肌肉猿爱写Java");
}
public static void main(String[] args) {
printString(str -> System.out.println(str));
}
}
分析上述代码:
printString方法是为了调用接口t1中的print方法,不用考虑接口中的方法的具体实现逻辑以及输出方式。在main方法中通过Lambda表达式指定了函数式接口t1的具体操作方式--------拿到String类型并在控制台中输出。经过分析我们发现,对于字符串在控制台中的输出方案在类的重载中获得了明确的实现,则可以省略不用手动调用。
改进后的代码形式
public class UseT1Test2 {
private static void printString(t1 data){
data.print("肌肉猿爱写Java");
}
public static void main(String[] args) {
printString(System.out::println);
}
}
b.方法引用符(::)详解
定义:上述的简洁lambda表达式双冒号::称为引用运算符,其所在表达式称之为方法引用
应用场景:加入Lambda表达式要表达的函数方案已经存在在某个方法的实现中,则可以通过双冒号引用该方法作为Lambda的替代者。
Ⅰ.关于语义运算符的语义分析
上述代码中,System.out对象方法重载了print(String)方法,则对于接口t1中的函数式接口参数以下两者方法完全等效。
- Lambda表达式写法:
s -> System.out.println(s);
- 方法引用写法:
System.out::println
第一种是拿到参数后通过Lambda之手,传递给输出语句进行打印输出。
ps:Lambda表达式传递的参数一定是接口方法中所书写的抽象方法可以接收的参数类型,不然会报错
第二种则是直接让System.out 中的printlnl来取代Lambda表达式,总之第二种方式复用了已有的方案更加简洁。
二、Lambda表达式各种方法引用
1.通过对象名引用成员变量
接口代码如下(示例):
@FunctionalInterface
public interface Printable {
void print(String str);
}
当一个类中已经存在一个成员方法
public class MethodRefObject {
public void printUpperCase(String str){
System.out.println(str.toUpperCase());
}
}
当我们需要使用类中printUpperCase成员方法来替代函数式接口的lambda,可以通过对象的实例来引用成员方法实现。
public class DemoMethodRef {
private static void printString(Printable lambda){
lambda.print("程序员非晚爱编程");
}
public static void main(String[] args) {
MethodRefObject demoMethodRef = new MethodRefObject();
printString(demoMethodRef::printUpperCase);
}
}
2.通过类名称引用静态方法
首先还是定义一个函数式接口
@FunctionalInterface
public interface Calcable {
int cal(int num);
}
使用Lambda表达式的写法示例
public class Demo01Lambda {
public static void method(int num,Calcable lambda){
System.out.println(lambda.cal(num));
}
public static void main(String[] args) {
method(-1314,n->Math.abs(n));
}
}
进阶写法就是使用方法引用
public class Demo02Lambda {
public static void method(int num,Calcable lambda){
System.out.println(lambda.cal(num));
}
public static void main(String[] args) {
method(-1314,Math::abs);
}
}
总结:在Java.lang.Math 中已经存在abs的静态方法
public static double abs(double a) {
return (a <= 0.0D) ? 0.0D - a : a;
}
上述的两种方式实际上是等效的
3.通过super引用成员方法
当类与类之间存在继承关系时,当Lambda表达式中出现super调用时,也可以使用方法引用进行替代。
首先定义函数式接口:
@FunctionalInterface
public interface Greetable {
void greet();
}
然后定义父类中的内容
public class Human {
public void sayHello(){
System.out.println("hello,你好");
}
}
最后定义子类Man中的内容,使用lambda表达式写法
public class Man extends Human{
@Override
public void sayHello() {
System.out.println("大家好,我是Man");
}
// 定义方法Method,参数传递Greetable接口
public void method(Greetable greetable){
greetable.greet();
}
public void show(){
// 调用method方法,使用lambda表达式
method(() -> new Human().sayHello());
// 简化lambda表达式 直接使用super关键字替代父类对象
method(() -> super.sayHello());
}
}
另一种写法是直接使用方法引用来调用父类中的sayHello方法
public class RealMan extends Human{
@Override
public void sayHello() {
System.out.println("大家好,我是真男人");
}
// 定义方法实现接口中的方法
public void method(Greetable greetable){
greetable.greet();
}
public void show(){
method(super :: sayHello);
}
}
4.通过this引用成员方法
声明this代表的就是当前的对象
首先定义函数式接口
@FunctionalInterface
public interface Human {
void buy();
}
定义一个类来调用接口中的方法
public class RealHuman {
private void marry(Human human){
human.buy();
}
public void tobebetterman(){
marry(()-> System.out.println("真男人要买套房"));
}
}
上述中成为更好男人方法调用了结婚方法,因为后者的参数为函数式接口,可以使用lambda表达式,当表达式的内容在本类中已经存在,可以使用this关键字替代
public class RealHuman {
private void buyHouse(){
System.out.println("真男人要买房");
}
private void marry(Human human){
human.buy();
}
public void tobebetterman(){
marry(()->this.buyHouse());
}
}
使用方法引用则更加简单,还不用写方法后边的括号了,示例如下
public class RealHuman {
private void buyHouse(){
System.out.println("真男人要买房");
}
private void marry(Human human){
human.buy();
}
public void tobebetterman(){
marry(this::buyHouse);
}
}
5.通过类的构造器引用
根据定义构造器的名称和类名完全一样,所以构造器引用可以使用类名称::new格式表示
首先定义一个类
public class Human {
private String name;
public Human(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后定义函数式接口
@FunctionalInterface
public interface Human {
RealHuman buildHuman(String name);
}
使用上述的函数式接口有两种方式,首先展示lambda表达式方式
public class LambdaHuman {
public static void printName(String name,Human builder){
System.out.println(builder.buildHuman(name).getName());
}
public static void main(String[] args) {
printName("肌肉猿是真男人",name -> new RealHuman(name));
}
}
另一种更加简洁的写法
public class RealHuman2 {
public static void printName(String name,Human builder){
System.out.println(builder.buildHuman(name).getName());
}
public static void main(String[] args) {
printName("肌肉猿是真男人",RealHuman::new);
}
}
此处的name -> new RealHuman(name)等价于RealHuman::new
6.数组构造器引用
声明:数组也是Object的子类,所以同样具有构造器,只是语法稍有不同
定义一个函数式接口
@FunctionalInterface
public interface ArrayBuilder {
int[] buildArray(int length);
}
使用lambda表达式应用接口
public class DemoArrayInitRef {
private static int[] initArray(int length,ArrayBuilder builder){
return builder.buildArray(length);
}
public static void main(String[] args) {
int[] array = initArray(10,length -> new int[length]);
}
}
使用数组的构造器引用实现
public class DemoArrayInitRef2 {
private static int[] initArray(int length,ArrayBuilder builder){
return builder.buildArray(length);
}
public static void main(String[] args) {
int[] array = initArray(10,int[]::new);
}
}