Java8 lambda支持
函數式編程
說lambdas前,先理解下什么是函數式編程,如果你是個純Java程序員,而且之前一直是沒有使用過Java8,可能還沒有使用過這種編程方式。用一句最直接的話解釋就是可以把函數當做參數傳入。舉個下面這樣的列子
int c1(int x,int y){return x+y; }void func(c1(int x,int y), // 參數一,這里相當于是把c1這個函數直接傳進來int c // 參數二 ){ // do something ...}上面的列子只是舉個簡單例子,Java中并沒有這樣的語法,下面用Java8的支持的lambdas語法演示下:
// 在Java8中使用lambdas方式,可以直接這樣寫: void func((x,y)->{x+y},int y) {// do something...}// (x,y)->x+y 這樣寫之前必須有一個這樣對應的接口是這樣定義的,如下 @FunctionalInterface // 這個注解不是必須的,只是為了表明這個接口是用于支持Lamdas函數 public interface Func{int c1(int x,int y); }// 在舉個使用異步線程的例子 new Thread(()->{// do something}).start() // 這里Runnable對象,就可以用lambdas表達式:()->{do something} // 當代碼只有一行的時候,可以不需要{}至于編譯器是怎樣解釋lambdas的語法的,我們先可以大膽猜測是把它編譯成一個匿名的對象,是不是可以這樣解釋且解釋的通,下面具體介紹下
lambda是什么
“Lambda 表達式”(lambda expression)是一個匿名函數,Lambda表達式基于數學中的λ演算得名,直接對應于其中的lambda抽象(lambda abstraction),是一個匿名函數,即沒有函數名的函數。Lambda表達式可以表示閉包(注意和數學傳統意義上的不同)。
可以理解為lamdba就是一種表達式語言,就是我們學習數學時,用一些符號來代表一些數學計算表達。
使用lambda的好處
- 支持函數式編程,我們在編程上多一種編程模式選擇,對于一些喜歡這種編程方式的人是個福音
- 使用lambda的地方,往往代碼會精簡很多,看起來不臃腫,易讀,有逼格
這是我個人使用后的一個感受
lambda在Java8中的使用
lambda是一種表達式語言,那我們常見可用的地方就是在一些數學計算描述中,如集合遍歷、排序,或者自定義一些lambda表達式,例如下面用于描述集合排序規則:
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia”); // (a,b)->a.compareTo(b) 可以這樣直接描述比較的規則 Collections.sort(names, (a,b)->a.compareTo(b));lambda的用法規則
怎樣編寫lambda表達式 ?寫法很簡單,下面這樣描述
params -> expression params -> {expression} //在表達式中可以通過::直接調用參數對象擁有的方法,如 a::length Lambda表達式編寫時可以自動參數類型,比如上面對names集合排序時,定義類型時List<String> (a,b)->a.compareTo(b) // 此時a,b的類型是String類型,你可以向下面這樣指定類型,但是多余的 (String a,String b)->a.compareTo(b) // 不用指定String類型修飾,可以自動推導什么時候可以使用lambda表達式?
Java中新增了一個注解:按照其解釋就是說,使用該注解注釋的接口都是函數接口,如果接口沒有使用該注解聲明,也會被當做函數接口。意思就是說,只要是接口類型,我們都可以傳入lambda表達式。在java.util.function包下定義了各種函數接口
/*** 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 {}總結
以上是生活随笔為你收集整理的Java8 lambda支持的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vundle按照YouComplete
- 下一篇: iOS 13 教程:如何在 iPhone