SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
Retrolambda
Use Lambdas in Android
kojilin@Cubie Inc.
2015/1/28
Lambda?
input.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
input.setOnClickListener(v -> {
...
});
Lambda Syntax
(int x, int y) -> x + y
(int x, int y) -> x + y
Argument List
(int x, int y) -> x + y
Arrow Token
(int x, int y) -> x + y
Body
(int x, int y) -> x + y
() -> 42
(String s) -> {
System.out.println(s);
}
● Body is expression
● Body is statement block
input.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
input.setOnClickListener(v -> {
...
});
input.setOnClickListener(v -> ...);
為何開心?
• 各種用到 anonymous class 的狀況
• Event listener
• Runnable
• RxJava
• Future / Promise Pattern API
input.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
input.setOnClickListener(v -> {
...
});
handler.post(new Runnable() {
@Override
public void run() {
...
}
});
handler.post(() -> {
...
});
Observable.from(new Integer[] {...})
.filter(new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer v) {
return v < 4;
}
}).subscribe(new Action1<Integer>() {
@Override
public void call(Integer value) {
System.out.println("Value: " + value);
}
});
Observable.from(new Integer[] {...})
.filter(v -> v < 4)
.subscribe(value -> {
System.out.println("Value: " + value);
});
設定只要幾個步驟
下載 JDK 8
build.gradle
buildscript {
...
dependencies {
classpath 'me.tatarka:gradle-retrolambda:2.5.0'
}
}
...
apply plugin: 'me.tatarka.retrolambda'
...
設定原始碼支援
Java 8 語法
• ProGuard
• -dontwarn java.lang.invoke.**
• 而且 IDE Debug feature 也沒問題
好像很不錯,
馬上導入 !?
好像很不錯,
馬上導入 !?
等一下!!!
怎麼做到的?
Java source
(.java)
Java bytecode
(.class)
Dalvik bytecode
(.dex)
Java source
(.java)
Java bytecode
(.class)
Dalvik bytecode
(.dex)
Java 8 bytecode Java 5/6/7 bytecode
• 未來 JDK 編譯出的 bytecode 改變
• 需等 retrolambda 支援
• 不支援的話,就得持續使用舊版 JDK
• Android 新的編譯過程不經過 bytecode
• Jack & Jill
• 新增修改程式碼有時會有 NoClassDefFoundError
• 未來 Android 上的 Java 語法應該(!?)會有 Java 8,所
以程式碼切換過去也不用改變
另外...
List<Integer> list = Arrays.asList(1,2,3);
Integer sum = list.stream()
.map(i -> i * 2)
.reduce(0, (x, y) -> x + y);
List<Integer> list = Arrays.asList(1,2,3);
Integer sum = list.stream()
.map(i -> i * 2)
.reduce(0, (x, y) -> x + y);
等一下!!!
• 因為是支援 lambda 語法,所以 java 8 的 API 並
沒有支援
• 可以使用 Guava, FunctionalJava 類的專案
Integer sum = fj.data.List.list(1,2,3)
.map(i -> i * 2)
.foldLeft(i -> (j -> i + j), 0);
除了 lambda 外其他好處
• Method reference
• Effective Final
• Generalized Target-Type Inference
input.setOnClickListener(System.out::println);

Contenu connexe

Tendances

C python 原始碼解析 投影片
C python 原始碼解析 投影片C python 原始碼解析 投影片
C python 原始碼解析 投影片kao kuo-tung
 
千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7Justin Lin
 
twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introductionotakustay
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试lydiafly
 
Jscex:案例、阻碍、体会、展望
Jscex:案例、阻碍、体会、展望Jscex:案例、阻碍、体会、展望
Jscex:案例、阻碍、体会、展望jeffz
 
Javascript share
Javascript shareJavascript share
Javascript shareXu Mac
 
Jscex:案例、经验、阻碍、展望
Jscex:案例、经验、阻碍、展望Jscex:案例、经验、阻碍、展望
Jscex:案例、经验、阻碍、展望jeffz
 
The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)jeffz
 
Python速成指南
Python速成指南Python速成指南
Python速成指南March Liu
 
Wind.js无障碍调试与排错
Wind.js无障碍调试与排错Wind.js无障碍调试与排错
Wind.js无障碍调试与排错jeffz
 
Js is js(程劭非) (1)
Js is js(程劭非) (1)Js is js(程劭非) (1)
Js is js(程劭非) (1)looneyren
 
Python learn guide
Python learn guidePython learn guide
Python learn guiderobin yang
 
Intro to C++ Basic
Intro to C++ BasicIntro to C++ Basic
Intro to C++ BasicShih Chi Lin
 
Sigreturn Oriented Programming
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented ProgrammingAngel Boy
 
Net Parallel Programming .NET平行處理與執行序
Net Parallel Programming .NET平行處理與執行序Net Parallel Programming .NET平行處理與執行序
Net Parallel Programming .NET平行處理與執行序HO-HSUN LIN
 
程式人雜誌 -- 2014 年5月號
程式人雜誌 -- 2014 年5月號程式人雜誌 -- 2014 年5月號
程式人雜誌 -- 2014 年5月號鍾誠 陳鍾誠
 

Tendances (20)

C python 原始碼解析 投影片
C python 原始碼解析 投影片C python 原始碼解析 投影片
C python 原始碼解析 投影片
 
千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7
 
twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introduction
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试
 
Jscex:案例、阻碍、体会、展望
Jscex:案例、阻碍、体会、展望Jscex:案例、阻碍、体会、展望
Jscex:案例、阻碍、体会、展望
 
Javascript share
Javascript shareJavascript share
Javascript share
 
Jscex:案例、经验、阻碍、展望
Jscex:案例、经验、阻碍、展望Jscex:案例、经验、阻碍、展望
Jscex:案例、经验、阻碍、展望
 
The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)The Evolution of Async Programming (GZ TechParty C#)
The Evolution of Async Programming (GZ TechParty C#)
 
Python速成指南
Python速成指南Python速成指南
Python速成指南
 
Wind.js无障碍调试与排错
Wind.js无障碍调试与排错Wind.js无障碍调试与排错
Wind.js无障碍调试与排错
 
Js is js(程劭非) (1)
Js is js(程劭非) (1)Js is js(程劭非) (1)
Js is js(程劭非) (1)
 
Python learn guide
Python learn guidePython learn guide
Python learn guide
 
Scala
ScalaScala
Scala
 
Python變數與資料運算
Python變數與資料運算Python變數與資料運算
Python變數與資料運算
 
Intro to C++ Basic
Intro to C++ BasicIntro to C++ Basic
Intro to C++ Basic
 
Sigreturn Oriented Programming
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented Programming
 
Execution
ExecutionExecution
Execution
 
Net Parallel Programming .NET平行處理與執行序
Net Parallel Programming .NET平行處理與執行序Net Parallel Programming .NET平行處理與執行序
Net Parallel Programming .NET平行處理與執行序
 
程式人雜誌 -- 2014 年5月號
程式人雜誌 -- 2014 年5月號程式人雜誌 -- 2014 年5月號
程式人雜誌 -- 2014 年5月號
 

Similaire à Use Lambdas in Android

竞赛中C++语言拾遗
竞赛中C++语言拾遗竞赛中C++语言拾遗
竞赛中C++语言拾遗乐群 陈
 
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版逸 張
 
基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册Zhen Li
 
论 Python 与设计模式。
论 Python 与设计模式。论 Python 与设计模式。
论 Python 与设计模式。勇浩 赖
 
VC++ Programming Training Lecture in Control Lab 301 of YSU
VC++ Programming Training Lecture in Control Lab 301 of YSUVC++ Programming Training Lecture in Control Lab 301 of YSU
VC++ Programming Training Lecture in Control Lab 301 of YSUGavin Gao
 
Java SE 7 技術手冊 - 課後練習解答
Java SE 7 技術手冊 - 課後練習解答Java SE 7 技術手冊 - 課後練習解答
Java SE 7 技術手冊 - 課後練習解答Justin Lin
 
xwz 2010-10-31
xwz 2010-10-31xwz 2010-10-31
xwz 2010-10-31carlxwz
 
Reactive X 响应式编程
Reactive X 响应式编程Reactive X 响应式编程
Reactive X 响应式编程Jun Liu
 

Similaire à Use Lambdas in Android (11)

竞赛中C++语言拾遗
竞赛中C++语言拾遗竞赛中C++语言拾遗
竞赛中C++语言拾遗
 
Ooredis
OoredisOoredis
Ooredis
 
Ooredis
OoredisOoredis
Ooredis
 
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
 
functional-scala
functional-scalafunctional-scala
functional-scala
 
基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册基于Eclipse和hadoop平台应用开发入门手册
基于Eclipse和hadoop平台应用开发入门手册
 
论 Python 与设计模式。
论 Python 与设计模式。论 Python 与设计模式。
论 Python 与设计模式。
 
VC++ Programming Training Lecture in Control Lab 301 of YSU
VC++ Programming Training Lecture in Control Lab 301 of YSUVC++ Programming Training Lecture in Control Lab 301 of YSU
VC++ Programming Training Lecture in Control Lab 301 of YSU
 
Java SE 7 技術手冊 - 課後練習解答
Java SE 7 技術手冊 - 課後練習解答Java SE 7 技術手冊 - 課後練習解答
Java SE 7 技術手冊 - 課後練習解答
 
xwz 2010-10-31
xwz 2010-10-31xwz 2010-10-31
xwz 2010-10-31
 
Reactive X 响应式编程
Reactive X 响应式编程Reactive X 响应式编程
Reactive X 响应式编程
 

Plus de koji lin

サーバーサイドでの非同期処理で色々やったよ
サーバーサイドでの非同期処理で色々やったよサーバーサイドでの非同期処理で色々やったよ
サーバーサイドでの非同期処理で色々やったよkoji lin
 
Using armeria to write your RPC
Using armeria to write your RPCUsing armeria to write your RPC
Using armeria to write your RPCkoji lin
 
使用 Java 上的 future/promise API
使用 Java 上的 future/promise  API使用 Java 上的 future/promise  API
使用 Java 上的 future/promise APIkoji lin
 
Annotation processing and code gen
Annotation processing and code genAnnotation processing and code gen
Annotation processing and code genkoji lin
 
docker intro
docker introdocker intro
docker introkoji lin
 
Java8 time
Java8 timeJava8 time
Java8 timekoji lin
 
Java8 stream
Java8 streamJava8 stream
Java8 streamkoji lin
 
CompletableFuture
CompletableFutureCompletableFuture
CompletableFuturekoji lin
 
Raspberry Pi with Java
Raspberry Pi with JavaRaspberry Pi with Java
Raspberry Pi with Javakoji lin
 
Services you can use to monitor and analyze mobile app
Services you can use to monitor and analyze mobile appServices you can use to monitor and analyze mobile app
Services you can use to monitor and analyze mobile appkoji lin
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Javakoji lin
 
山頂洞人日記 - 回歸到最純樸的開發
山頂洞人日記 -  回歸到最純樸的開發山頂洞人日記 -  回歸到最純樸的開發
山頂洞人日記 - 回歸到最純樸的開發koji lin
 
Android Location-based應用開發分享
Android Location-based應用開發分享Android Location-based應用開發分享
Android Location-based應用開發分享koji lin
 

Plus de koji lin (17)

サーバーサイドでの非同期処理で色々やったよ
サーバーサイドでの非同期処理で色々やったよサーバーサイドでの非同期処理で色々やったよ
サーバーサイドでの非同期処理で色々やったよ
 
G1GC
G1GCG1GC
G1GC
 
Using armeria to write your RPC
Using armeria to write your RPCUsing armeria to write your RPC
Using armeria to write your RPC
 
使用 Java 上的 future/promise API
使用 Java 上的 future/promise  API使用 Java 上的 future/promise  API
使用 Java 上的 future/promise API
 
Annotation processing and code gen
Annotation processing and code genAnnotation processing and code gen
Annotation processing and code gen
 
Jcconf
JcconfJcconf
Jcconf
 
docker intro
docker introdocker intro
docker intro
 
Java8 time
Java8 timeJava8 time
Java8 time
 
Java8 stream
Java8 streamJava8 stream
Java8 stream
 
Idea13
Idea13Idea13
Idea13
 
CompletableFuture
CompletableFutureCompletableFuture
CompletableFuture
 
Raspberry Pi with Java
Raspberry Pi with JavaRaspberry Pi with Java
Raspberry Pi with Java
 
Services you can use to monitor and analyze mobile app
Services you can use to monitor and analyze mobile appServices you can use to monitor and analyze mobile app
Services you can use to monitor and analyze mobile app
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
 
JQuery
JQueryJQuery
JQuery
 
山頂洞人日記 - 回歸到最純樸的開發
山頂洞人日記 -  回歸到最純樸的開發山頂洞人日記 -  回歸到最純樸的開發
山頂洞人日記 - 回歸到最純樸的開發
 
Android Location-based應用開發分享
Android Location-based應用開發分享Android Location-based應用開發分享
Android Location-based應用開發分享
 

Use Lambdas in Android