对于刚接触Lambda表达式的开发者来说,这个概念是比较晦涩的,下面我来写一下我的理解。
一,简介
从JDK1.8开始,Java加入了Lambda表达式的设计,并加入了一个新的操作符:->,操作符前面代表方法参数列表,操作符后面是Lambda体,代表方法的实现代码。
Lambda表达式说的是:当一个接口(interface)里面,除了static方法和default方法之外只有一个方法时,我们可以用Lambda表达式来快速的创建这个接口的实现类,同时定义接口里这个唯一方法的实现代码,当我们调用接口的这个唯一方法时,实际调用的就是我们自定义的实现代码,这个思路就像是在创建一个内部类一样。
使用Lambda表达式,代码量比不用Lambda表达式时要少很多。
上面提到的只有一个方法的接口,叫做函数式接口,可以是自定义的接口,也可以是Java自带的,比如常见的java.lang.Runnable接口:
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
可见,Runnable接口里面只有一个run()方法,那么当我们使用Runnable接口时,就可以使用Lambda表达式。
二,一个例子
以上面的Runnable接口为例,Runnable接口没有参数,也没有返回值,属于最简单的场景,我们可以这样用:
public static void main(String[] args) {
Runnable runnable = () -> System.out.println("启动");
runnable.run();
}
上面的代码中使用了Lambda表达式:
() -> System.out.println("启动")
这个Lambda表达式的作用是:
1,创建了一个Runnable接口的实现类并且返回了他的一个对象。
2,操作符前面的()代表方法没有参数。Runnable接口的run()方法确实不需要参数。
3,操作符后面的System.out.println("启动");是我们定义的run()方法的实现代码,实现代码就这一行。
4,当我们调用Runnable接口中的唯一方法:
runnable.run();
这行代码时,调用的实际上是我们自定义的:
System.out.println("启动");
这段代码。
因此,以上的代码如果不用Lambda表达式的话,是这么写的:
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("启动");
}
};
runnable.run();
}
可见使用Lambda表达式确实节省了很多代码量。
不过在代码逻辑很复杂的情况下,代码可能会变的有点难读。
三,Lambda表达式写法的简化
1,较为复杂的使用方法
我们定义一个较为复杂的接口ImTheOne,里面方法依然只有一个,方法有参数,有返回值,实现方法里面有多行:
public interface ImTheOne {
String fly(String a, String b);
}
public class Test {
public static void main(String[] args) {
ImTheOne theOne = (String a, String b) -> {
String c = a + b;
return c;
};
String result = theOne.fly("aaa", "bbb");
System.out.println(result);
}
}
输出结果是:abcdef
这是相对复杂一些的场景,如果场景简单一些,Lambda表达式的写法也可以简化。
2,参数列表中的数据类型可以省略
Lambda表达式中参数列表的数据类型可以不写,JVM自己会匹配类型,比如这样:
public interface ImTheOne {
String fly(String a, String b);
}
public class Test {
public static void main(String[] args) {
ImTheOne theOne = (a, b) -> {
String c = a + b;
return c;
};
String result = theOne.fly("abc", "def");
System.out.println(result);
}
}
此时Lambda参数列表写的是(a,b),没有数据类型。
另外,数据类型如果不写,那就所有参数都不写,不能有的写有的不写,否则会有编译错误。
3,如果方法参数只有一个,参数列表的括号可以不写
方法参数只有一个时,可以这样写:
public interface ImTheOne {
String fly(String a);
}
public class Test {
public static void main(String[] args) {
ImTheOne theOne = a -> {
String c = a + "def";
return c;
};
String result = theOne.fly("abc");
System.out.println(result);
}
}
4,方法没有返回值,且实现方法只有一行时,可以不写大括号
这种情况可以这样写:
public interface ImTheOne {
void fly(String a);
}
public class Test {
public static void main(String[] args) {
ImTheOne theOne = a -> System.out.println(a + "def");
theOne.fly("abc");
}
}
5,方法有返回值,且实现方法只有一行时,可以不写大括号,甚至不用写return关键字
这种情况可以这样写:
public interface ImTheOne {
String fly(String a);
}
public class Test {
public static void main(String[] args) {
ImTheOne theOne = a -> a + "def";
String result = theOne.fly("abc");
System.out.println(result);
}
}
可以看到Lambda表达式:
a -> a + "def"
不用写return,其实意思是:
return a + "def";
不写大括号的情况下,写了return反而会有编译错误。
四,Spring中使用Lambda表达式的一个例子
sharedInstance = this.getSingleton(beanName, () -> {
try {
return this.createBean(beanName, mbd, args);
} catch (BeansException var5) {
this.destroySingleton(beanName);
throw var5;
}
});
这段代码在org.springframework.beans.factory.support.AbstractBeanFactory的doGetBean方法中,这段代码中用到的Lambda表达式其实是成为了getSingleton方法的第二个参数,这个getSingleton方法的定义是这样的:
getSingleton(String beanName, ObjectFactory<?> singletonFactory)
我们看到第二个参数是ObjectFactory对象,这个ObjectFactory是一个接口:
@FunctionalInterface
public interface ObjectFactory<T> {
/**
* Return an instance (possibly shared or independent)
* of the object managed by this factory.
* @return the resulting instance
* @throws BeansException in case of creation errors
*/
T getObject() throws BeansException;
}
只有一个方法getObject()。
因此,Spring中这段代码要表达的是,在this.getSingleton()方法中,如果调用
singletonFactory. getObject();
那么执行的代码就会是Lambda表达式中定义的:
try {
return this.createBean(beanName, mbd, args);
} catch (BeansException var5) {
this.destroySingleton(beanName);
throw var5;
}
这段代码。
以上