Java8 lambda支持
函數(shù)式編程
說lambdas前,先理解下什么是函數(shù)式編程,如果你是個(gè)純Java程序員,而且之前一直是沒有使用過Java8,可能還沒有使用過這種編程方式。用一句最直接的話解釋就是可以把函數(shù)當(dāng)做參數(shù)傳入。舉個(gè)下面這樣的列子
int c1(int x,int y){return x+y; }void func(c1(int x,int y), // 參數(shù)一,這里相當(dāng)于是把c1這個(gè)函數(shù)直接傳進(jìn)來int c // 參數(shù)二 ){ // do something ...}上面的列子只是舉個(gè)簡(jiǎn)單例子,Java中并沒有這樣的語法,下面用Java8的支持的lambdas語法演示下:
// 在Java8中使用lambdas方式,可以直接這樣寫: void func((x,y)->{x+y},int y) {// do something...}// (x,y)->x+y 這樣寫之前必須有一個(gè)這樣對(duì)應(yīng)的接口是這樣定義的,如下 @FunctionalInterface // 這個(gè)注解不是必須的,只是為了表明這個(gè)接口是用于支持Lamdas函數(shù) public interface Func{int c1(int x,int y); }// 在舉個(gè)使用異步線程的例子 new Thread(()->{// do something}).start() // 這里Runnable對(duì)象,就可以用lambdas表達(dá)式:()->{do something} // 當(dāng)代碼只有一行的時(shí)候,可以不需要{}至于編譯器是怎樣解釋lambdas的語法的,我們先可以大膽猜測(cè)是把它編譯成一個(gè)匿名的對(duì)象,是不是可以這樣解釋且解釋的通,下面具體介紹下
lambda是什么
“Lambda 表達(dá)式”(lambda expression)是一個(gè)匿名函數(shù),Lambda表達(dá)式基于數(shù)學(xué)中的λ演算得名,直接對(duì)應(yīng)于其中的lambda抽象(lambda abstraction),是一個(gè)匿名函數(shù),即沒有函數(shù)名的函數(shù)。Lambda表達(dá)式可以表示閉包(注意和數(shù)學(xué)傳統(tǒng)意義上的不同)。
可以理解為lamdba就是一種表達(dá)式語言,就是我們學(xué)習(xí)數(shù)學(xué)時(shí),用一些符號(hào)來代表一些數(shù)學(xué)計(jì)算表達(dá)。
使用lambda的好處
- 支持函數(shù)式編程,我們?cè)诰幊躺隙嘁环N編程模式選擇,對(duì)于一些喜歡這種編程方式的人是個(gè)福音
- 使用lambda的地方,往往代碼會(huì)精簡(jiǎn)很多,看起來不臃腫,易讀,有逼格
這是我個(gè)人使用后的一個(gè)感受
lambda在Java8中的使用
lambda是一種表達(dá)式語言,那我們常見可用的地方就是在一些數(shù)學(xué)計(jì)算描述中,如集合遍歷、排序,或者自定義一些lambda表達(dá)式,例如下面用于描述集合排序規(guī)則:
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia”); // (a,b)->a.compareTo(b) 可以這樣直接描述比較的規(guī)則 Collections.sort(names, (a,b)->a.compareTo(b));lambda的用法規(guī)則
怎樣編寫lambda表達(dá)式 ?寫法很簡(jiǎn)單,下面這樣描述
params -> expression params -> {expression} //在表達(dá)式中可以通過::直接調(diào)用參數(shù)對(duì)象擁有的方法,如 a::length Lambda表達(dá)式編寫時(shí)可以自動(dòng)參數(shù)類型,比如上面對(duì)names集合排序時(shí),定義類型時(shí)List<String> (a,b)->a.compareTo(b) // 此時(shí)a,b的類型是String類型,你可以向下面這樣指定類型,但是多余的 (String a,String b)->a.compareTo(b) // 不用指定String類型修飾,可以自動(dòng)推導(dǎo)什么時(shí)候可以使用lambda表達(dá)式?
Java中新增了一個(gè)注解:按照其解釋就是說,使用該注解注釋的接口都是函數(shù)接口,如果接口沒有使用該注解聲明,也會(huì)被當(dāng)做函數(shù)接口。意思就是說,只要是接口類型,我們都可以傳入lambda表達(dá)式。在java.util.function包下定義了各種函數(shù)接口
/*** An informative annotation type used to indicate that an interface* type declaration is intended to be a <i>functional interface</i> as* defined by the Java Language Specification.** Conceptually, a functional interface has exactly one abstract* method. Since {@linkplain java.lang.reflect.Method#isDefault()* default methods} have an implementation, they are not abstract. If* an interface declares an abstract method overriding one of the* public methods of {@code java.lang.Object}, that also does* <em>not</em> count toward the interface's abstract method count* since any implementation of the interface will have an* implementation from {@code java.lang.Object} or elsewhere.** <p>Note that instances of functional interfaces can be created with* lambda expressions, method references, or constructor references.** <p>If a type is annotated with this annotation type, compilers are* required to generate an error message unless:** <ul>* <li> The type is an interface type and not an annotation type, enum, or class.* <li> The annotated type satisfies the requirements of a functional interface.* </ul>** <p>However, the compiler will treat any interface meeting the* definition of a functional interface as a functional interface* regardless of whether or not a {@code FunctionalInterface}* annotation is present on the interface declaration.** @jls 4.3.2. The Class Object* @jls 9.8 Functional Interfaces* @jls 9.4.3 Interface Method Body* @since 1.8*/ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface FunctionalInterface {}總結(jié)
以上是生活随笔為你收集整理的Java8 lambda支持的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vundle按照YouComplete
- 下一篇: 博为峰Java技术文章 ——JavaSE