java8 Lambda表達式使用 以及變量的使用限制 方法引用

 

筆記地址

lambda使用摘要

 

1、lambdas in a nutshell

1、簡而言之

2、lambda 表達式組成 參數 箭頭 方法體

3、lambda案例

2、where and how to use lambdas

1、Functional Interfaces

2、Function descriptor

3、the execute-around pattern

4、Functionnal interfaces

Functional interfaces

1、Precidate

2、Consumer

3、Function

5、Type interface,Type checking, restrications

1、type checking

2、type inference

3、local variables restrictions

閉包

6、Method refernces

1、方法引用三種表達方式

2、構造方法引用

1、無參方法調用

2、有參方法調用

3、實際使用

7、Composing lambdas

1、Composing Predicate

2、Composing Function

1、lambdas in a nutshell

1、簡而言之

可以把lambda 想象爲一個匿名內部類,一個沒有聲明名字的方法,但是可以作爲參數在方法中傳遞

 Anonymous—We say anonymous because it doesn’t have an explicit name like a

method would normally have; less to write and think about!

 Function—We say function because a lambda isn’t associated with a particular

class like a method is. But like a method, a lambda has a list of parameters, a

body, a return type, and a possible list of exceptions that can be thrown.

 Passed around—A lambda expression can be passed as argument to a method or

stored in a variable.

 Concise—You don’t need to write a lot of boilerplate like you do for anonymous

classes

2、lambda 表達式組成 參數 箭頭 方法體

 

3、lambda案例

lambda還有另外一種寫法

  1. ()->{} 無參並且返回void 類似於 pubic void run(){}
  2. ()-> "July" 無參並且返回String 但是省略關鍵字 return
  3. ()->{return "July";} 無參 並且帶有return 顯示返回 因爲在代碼塊裏
  4. (Integer i)->return "July"+i ; 錯誤寫法 應爲 (Integer i)->{return "July"+i };
  5. (String s)-> {"July";} 錯誤寫法 應爲 (String s)-> { return "July";} 或者 (String s)-> "July"

 

 

2、where and how to use lambdas

1、Functional Interfaces

接口裏只有一個抽象方法 鑑於java8引入了default 方法 如果一個接口有很多default方法但是隻有一個抽象方法 它也是一個功能接口

 

2、Function descriptor

3、the execute-around pattern

java 7 引入 try(){} 在try後面()裏寫創建資源鏈接,可以無需手動釋放連接。

4、Functionnal interfaces

Functional interfaces

1、Precidate

 

2、Consumer

3、Function

5、Type interface,Type checking, restrications

1、type checking

 

強制類型轉換

 

2、type inference

 

3、local variables restrictions

https://blog.csdn.net/yangyifei2014/article/details/80068265

對於基本類型,是指不能改變它的值;對於引用類型,是指不能改變它的指向

lambda 允許使用final修飾的外部變量(Effectively final 編譯器會幫你把表達式變量轉換爲final類型)或者是

未經final修飾的實例化變量

因爲局部變量是線程私有,處於棧上,java使用的是值拷貝引用並不是直接使用原值,對於拷貝值的修改,並不會影響到原變量的值,如果不是final 那麼外部變量在lambda中是不能被改變的,但是如果是final修飾的

但是實例化變量是處於堆上,爲線程所共享

 

錯誤實例

 

閉包

其他語言的閉包是指一個實例化的函數可以可不受限制地使用該函數的非局部變量

java8 的lambda以及匿名內部類有類似於閉包的功能:

他們可以作爲參數傳遞給方法並可以訪問其作用域之外的變量(nonLocal,java不支持引用傳遞),但是有限制,不能修改lambda表達式所在方法的佈局變量的內容,如果可以使用可變變量,會帶來線程不安全。

 

 

6、Method refernces

1、方法引用三種表達方式

  • 類名::靜態方法名
  • 類名::任意方法名
  • 實例化變量的引用::方法名

 

2、構造方法引用

1、無參方法調用

類名::new

 

2、有參方法調用

  1. 1個參數

 

  1. 2個參數

 

3、實際使用

 

 

7、Composing lambdas

 

1、Composing Predicate

negate

and

or

 

2、Composing Function

鏈式調用 第一個函數的結果傳給第二個函數

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

http://note.youdao.com/noteshare?id=73a154ba59b53f6be0d7d8c4638b4e1a&sub=85B8294998224AA0873B24CA7368408A

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章