對於剛接觸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;
}
這段代碼。
以上