SlideShare a Scribd company logo
1 of 21
Download to read offline
Using Java 8 on
Android
Eduardo Bonet
Motivation
Java 8 was released over 2 years ago, bringing many new features, but
that are not yet availabe on Android.
The new Jack & Kill toolchain brought us some official Java 8 support,
but some features are only available after Android N and others were left
aside.
Java 8
Much less verbose.
New API's:
java.time.*
java.util.stream.*
java.util.function.*
Language changes:
Default Methods and static methods for interfaces
Lambda Functions
Method References
Streams
Improved API for dealing with collections, also making parallelization much
easier.
// Java 7
names = new ArrayList<>();
for (Person p : people) {
if(p.age > 16)
names.add(p.name);
}
// Java 8
names = people.stream()
.filter(p -> p.age > 16)
.map(p -> p.name)
.collect(Collectors.toList());
Time API
New API to deal with date and time, fully replacing java.util.Calendar and
java.util.Date.
// Java7
Date date, datePlusThreeDays;
date = new GregorianCalendar(2014, Calendar.FEBRUARY, 11).getTime()
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, 3)
datePlusThreeDays = c.getTime()
// Java 8
LocalDate otherDate, otherDatePlusThreeDays;
otherDate = LocalDate.of(2014, Month.FEBRUARY, 11);
otherDatePlusThreeDays = otherDate.plus(3, ChronoUnit.DAYS);
Lambda Functions
Lambda Functions are a easier and cleaner way to create objects that
implement a single method interface, i. e., Functors.
// Java 7
v.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Log.d(TAG, "onClick: ");
}
});
// Java 8 Lambda
v.setOnClickListener(view -> Log.d(TAG, "onClick: "));
Observable.from(people)
.filter(new Func1<Person, Boolean>() {
@Override
public Boolean call(Person person) {
return person.age > 16;
}
})
.map(new Func1<Person, String>() {
@Override
public String call(Person person) {
return person.name;
}
})
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
System.out.println(s);
}
});
Observable.from(people)
.filter(person -> person.age > 16)
.map(person -> person.name)
.subscribe(s -> System.out.println(s));
Method References
Method References are an even simpler version of Lambda Functions,
where arguments are simply passed on to another function.
Observable.from(people)
.filter(person -> person.age > 16)
.map(person -> person.name)
.subscribe(System.out::println); // .subscribe(s -> System.out.println(s));
Try-with-resources
Better Syntax for objects that must be closed after use (they must
implement the Closeable interface).
// Java 7
BufferedReader br = new BufferedReader(new FileReader(path));
try {
System.out.println(br.readLine());
} finally {
if (br != null) br.close();
}
// Java 8
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
System.out.println(br.readLine());
}
Default Methods for Interfaces
interface Vehicle {
default void print(){
System.out.println("I am a vehicle!");
}
}
class Car implements Vehicle {
public void print(){
Vehicle.super.print();
System.out.println("I am a car!");
}
}
class Boat implements Vehicle {
}
Bringing Java 8 to Android
Jack
New Android tool that transforms .java into .dex
Introduced on Android N.
Suports Lambda Functions and Method References on all Android
versions.
Suports Default Methods and Streams only for Android 24+
Does nto support java.time.*
Streams: LightweightStreams
Streams API implementation using Java 7 Collections.
List<String> names = Stream.of(people)
.filter(p -> p.age > 16)
.map(p -> p.name)
.collect(Collectors.toList());
Method Count: 719 (1.1.2)
Streams: RxJava
Observables are fundamentally different from Streams (push vs. pull), but
similar functionality can be obtained using Observable.from(myCollection).
List<String> names = Observable.from(people)
.filter(p -> p.age > 16)
.map(p -> p.name)
.toList().toBlocking().first();
Method Count: 5492 (1.1.8)
Streams
// Stream
people.stream()
.filter(p -> p.age > 16)
.map(p -> p.name)
.collect(Collectors.toList());
// LightweightStreams
Stream.of(people)
.filter(p -> p.age > 16)
.map(p -> p.name)
.collect(Collectors.toList());
// RxJava
Observable.from(people)
.filter(p -> p.age > 16)
.map(p -> p.name)
.toList().toBlocking().first();
java.time.*:
ThreeTenABP
optimized version for Android.
Same API as java.time.*, making them interchangeable.
Method Count: 3280
ThreeTenBP
Retrolambda
Transforms Java 8 code into code compatible with Java 5, 6 and 7.
Operates during compile time.
Full support to Lambdas, Try-With-Resources and Method References.
Partial support to default methods.
RetroLambda vs Jack
https://speakerdeck.com/jakewharton/exploring-hidden-java-costs-360-andev-july-2016?slide=126
Resumo
RL Jack RxJava LS TT
Streams 24+ ✔ ✔
Default Methods Partial 24+
Lambda ✔ ✔
Method References ✔ ✔
Try-With-Resources ✔ ✔
java.time.* ✔
- RL: Retrolambda, LS: LightweightStreams, TT: ThreeTenABP
References
Jack
Jack e Java 8
Retrolambda
Lightweight Stream
ThreeTenABP
Estudo sobre API para date
RxJava
Retrolambda vs Jack
Thanks
Questions?
| |blog github linkedin

More Related Content

What's hot

Getting the Most From Modern Java
Getting the Most From Modern JavaGetting the Most From Modern Java
Getting the Most From Modern JavaSimon Ritter
 
re-frame à la spec
re-frame à la specre-frame à la spec
re-frame à la specKent Ohashi
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Anton Arhipov
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Ganesh Samarthyam
 
Zen and the Art of REST API documentation - MuCon London 2015
Zen and the Art of REST API documentation - MuCon London 2015Zen and the Art of REST API documentation - MuCon London 2015
Zen and the Art of REST API documentation - MuCon London 2015Steve Judd
 
Communication between Java and Python
Communication between Java and PythonCommunication between Java and Python
Communication between Java and PythonAndreas Schreiber
 
Practical REPL-driven Development with Clojure
Practical REPL-driven Development with ClojurePractical REPL-driven Development with Clojure
Practical REPL-driven Development with ClojureKent Ohashi
 
[PyCon 2014 APAC] How to integrate python into a scala stack to build realtim...
[PyCon 2014 APAC] How to integrate python into a scala stack to build realtim...[PyCon 2014 APAC] How to integrate python into a scala stack to build realtim...
[PyCon 2014 APAC] How to integrate python into a scala stack to build realtim...Jerry Chou
 
Bytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMBytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMashleypuls
 
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Anton Arhipov
 
In search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testingIn search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testingAnna Khabibullina
 
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Anton Arhipov
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIGanesh Samarthyam
 
Kotlin in action
Kotlin in actionKotlin in action
Kotlin in actionCiro Rizzo
 
Introduction to OpenCV 3.x (with Java)
Introduction to OpenCV 3.x (with Java)Introduction to OpenCV 3.x (with Java)
Introduction to OpenCV 3.x (with Java)Luigi De Russis
 

What's hot (20)

Getting the Most From Modern Java
Getting the Most From Modern JavaGetting the Most From Modern Java
Getting the Most From Modern Java
 
re-frame à la spec
re-frame à la specre-frame à la spec
re-frame à la spec
 
Developing android apps with java 8
Developing android apps with java 8Developing android apps with java 8
Developing android apps with java 8
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
Zen and the Art of REST API documentation - MuCon London 2015
Zen and the Art of REST API documentation - MuCon London 2015Zen and the Art of REST API documentation - MuCon London 2015
Zen and the Art of REST API documentation - MuCon London 2015
 
JVM
JVMJVM
JVM
 
Java 8 Overview
Java 8 OverviewJava 8 Overview
Java 8 Overview
 
Communication between Java and Python
Communication between Java and PythonCommunication between Java and Python
Communication between Java and Python
 
Practical REPL-driven Development with Clojure
Practical REPL-driven Development with ClojurePractical REPL-driven Development with Clojure
Practical REPL-driven Development with Clojure
 
[PyCon 2014 APAC] How to integrate python into a scala stack to build realtim...
[PyCon 2014 APAC] How to integrate python into a scala stack to build realtim...[PyCon 2014 APAC] How to integrate python into a scala stack to build realtim...
[PyCon 2014 APAC] How to integrate python into a scala stack to build realtim...
 
Bytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMBytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASM
 
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011
 
Java vs. C/C++
Java vs. C/C++Java vs. C/C++
Java vs. C/C++
 
In search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testingIn search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testing
 
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
 
Kotlin in action
Kotlin in actionKotlin in action
Kotlin in action
 
55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
 
Introduction to OpenCV 3.x (with Java)
Introduction to OpenCV 3.x (with Java)Introduction to OpenCV 3.x (with Java)
Introduction to OpenCV 3.x (with Java)
 

Viewers also liked

JDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerJDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerSimon Ritter
 
I/O Extended (GDG Bogor) - Andrew Kurniadi
I/O Extended (GDG Bogor) - Andrew KurniadiI/O Extended (GDG Bogor) - Andrew Kurniadi
I/O Extended (GDG Bogor) - Andrew KurniadiDicoding
 
Java 8 and 9 in Anger
Java 8 and 9 in AngerJava 8 and 9 in Anger
Java 8 and 9 in AngerTrisha Gee
 
Java 9 Functionality and Tooling
Java 9 Functionality and ToolingJava 9 Functionality and Tooling
Java 9 Functionality and ToolingTrisha Gee
 
Migrating to IntelliJ IDEA from Eclipse
Migrating to IntelliJ IDEA from EclipseMigrating to IntelliJ IDEA from Eclipse
Migrating to IntelliJ IDEA from EclipseTrisha Gee
 
Modularization With Project Jigsaw in JDK 9
Modularization With Project Jigsaw in JDK 9Modularization With Project Jigsaw in JDK 9
Modularization With Project Jigsaw in JDK 9Simon Ritter
 
Java 9 – The Ultimate Feature List
Java 9 – The Ultimate Feature ListJava 9 – The Ultimate Feature List
Java 9 – The Ultimate Feature ListTakipi
 
55 New Features in JDK 9
55 New Features in JDK 955 New Features in JDK 9
55 New Features in JDK 9Simon Ritter
 

Viewers also liked (10)

Usando Java 8 no Android
Usando Java 8 no AndroidUsando Java 8 no Android
Usando Java 8 no Android
 
JDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerJDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java Smaller
 
I/O Extended (GDG Bogor) - Andrew Kurniadi
I/O Extended (GDG Bogor) - Andrew KurniadiI/O Extended (GDG Bogor) - Andrew Kurniadi
I/O Extended (GDG Bogor) - Andrew Kurniadi
 
Java 8 and 9 in Anger
Java 8 and 9 in AngerJava 8 and 9 in Anger
Java 8 and 9 in Anger
 
What's New in Java SE 9
What's New in Java SE 9What's New in Java SE 9
What's New in Java SE 9
 
Java 9 Functionality and Tooling
Java 9 Functionality and ToolingJava 9 Functionality and Tooling
Java 9 Functionality and Tooling
 
Migrating to IntelliJ IDEA from Eclipse
Migrating to IntelliJ IDEA from EclipseMigrating to IntelliJ IDEA from Eclipse
Migrating to IntelliJ IDEA from Eclipse
 
Modularization With Project Jigsaw in JDK 9
Modularization With Project Jigsaw in JDK 9Modularization With Project Jigsaw in JDK 9
Modularization With Project Jigsaw in JDK 9
 
Java 9 – The Ultimate Feature List
Java 9 – The Ultimate Feature ListJava 9 – The Ultimate Feature List
Java 9 – The Ultimate Feature List
 
55 New Features in JDK 9
55 New Features in JDK 955 New Features in JDK 9
55 New Features in JDK 9
 

Similar to Using Java 8 on Android

New Features of JAVA SE8
New Features of JAVA SE8New Features of JAVA SE8
New Features of JAVA SE8Dinesh Pathak
 
Java 8 - Lambdas and much more
Java 8 - Lambdas and much moreJava 8 - Lambdas and much more
Java 8 - Lambdas and much moreAlin Pandichi
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsEmiel Paasschens
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 itiAhmed mar3y
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014Nayden Gochev
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 
Java 9 features
Java 9 featuresJava 9 features
Java 9 featuresshrinath97
 
Java features. Java 8, 9, 10, 11
Java features. Java 8, 9, 10, 11Java features. Java 8, 9, 10, 11
Java features. Java 8, 9, 10, 11Ivelin Yanev
 
Alive and Well with Java 8
Alive and Well with Java 8Alive and Well with Java 8
Alive and Well with Java 8Adam Pelsoczi
 
whats new in java 8
whats new in java 8 whats new in java 8
whats new in java 8 Dori Waldman
 
DevNexus 2020: Discover Modern Java
DevNexus 2020: Discover Modern JavaDevNexus 2020: Discover Modern Java
DevNexus 2020: Discover Modern JavaHenri Tremblay
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1sotlsoc
 

Similar to Using Java 8 on Android (20)

New Features of JAVA SE8
New Features of JAVA SE8New Features of JAVA SE8
New Features of JAVA SE8
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Java 8 - Lambdas and much more
Java 8 - Lambdas and much moreJava 8 - Lambdas and much more
Java 8 - Lambdas and much more
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
java new technology
java new technologyjava new technology
java new technology
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 iti
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Java 9 features
Java 9 featuresJava 9 features
Java 9 features
 
Java features. Java 8, 9, 10, 11
Java features. Java 8, 9, 10, 11Java features. Java 8, 9, 10, 11
Java features. Java 8, 9, 10, 11
 
Java 7 & 8 New Features
Java 7 & 8 New FeaturesJava 7 & 8 New Features
Java 7 & 8 New Features
 
Alive and Well with Java 8
Alive and Well with Java 8Alive and Well with Java 8
Alive and Well with Java 8
 
whats new in java 8
whats new in java 8 whats new in java 8
whats new in java 8
 
Java 8-revealed
Java 8-revealedJava 8-revealed
Java 8-revealed
 
DevNexus 2020: Discover Modern Java
DevNexus 2020: Discover Modern JavaDevNexus 2020: Discover Modern Java
DevNexus 2020: Discover Modern Java
 
It's always your fault
It's always your faultIt's always your fault
It's always your fault
 
Chapter 2.1
Chapter 2.1Chapter 2.1
Chapter 2.1
 
Basic java part_ii
Basic java part_iiBasic java part_ii
Basic java part_ii
 
Json generation
Json generationJson generation
Json generation
 

Recently uploaded

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 

Recently uploaded (20)

Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 

Using Java 8 on Android

  • 1. Using Java 8 on Android Eduardo Bonet
  • 2. Motivation Java 8 was released over 2 years ago, bringing many new features, but that are not yet availabe on Android. The new Jack & Kill toolchain brought us some official Java 8 support, but some features are only available after Android N and others were left aside.
  • 3. Java 8 Much less verbose. New API's: java.time.* java.util.stream.* java.util.function.* Language changes: Default Methods and static methods for interfaces Lambda Functions Method References
  • 4. Streams Improved API for dealing with collections, also making parallelization much easier. // Java 7 names = new ArrayList<>(); for (Person p : people) { if(p.age > 16) names.add(p.name); } // Java 8 names = people.stream() .filter(p -> p.age > 16) .map(p -> p.name) .collect(Collectors.toList());
  • 5. Time API New API to deal with date and time, fully replacing java.util.Calendar and java.util.Date. // Java7 Date date, datePlusThreeDays; date = new GregorianCalendar(2014, Calendar.FEBRUARY, 11).getTime() Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DATE, 3) datePlusThreeDays = c.getTime() // Java 8 LocalDate otherDate, otherDatePlusThreeDays; otherDate = LocalDate.of(2014, Month.FEBRUARY, 11); otherDatePlusThreeDays = otherDate.plus(3, ChronoUnit.DAYS);
  • 6. Lambda Functions Lambda Functions are a easier and cleaner way to create objects that implement a single method interface, i. e., Functors. // Java 7 v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d(TAG, "onClick: "); } }); // Java 8 Lambda v.setOnClickListener(view -> Log.d(TAG, "onClick: "));
  • 7. Observable.from(people) .filter(new Func1<Person, Boolean>() { @Override public Boolean call(Person person) { return person.age > 16; } }) .map(new Func1<Person, String>() { @Override public String call(Person person) { return person.name; } }) .subscribe(new Action1<String>() { @Override public void call(String s) { System.out.println(s); } }); Observable.from(people) .filter(person -> person.age > 16) .map(person -> person.name) .subscribe(s -> System.out.println(s));
  • 8. Method References Method References are an even simpler version of Lambda Functions, where arguments are simply passed on to another function. Observable.from(people) .filter(person -> person.age > 16) .map(person -> person.name) .subscribe(System.out::println); // .subscribe(s -> System.out.println(s));
  • 9. Try-with-resources Better Syntax for objects that must be closed after use (they must implement the Closeable interface). // Java 7 BufferedReader br = new BufferedReader(new FileReader(path)); try { System.out.println(br.readLine()); } finally { if (br != null) br.close(); } // Java 8 try (BufferedReader br = new BufferedReader(new FileReader(path))) { System.out.println(br.readLine()); }
  • 10. Default Methods for Interfaces interface Vehicle { default void print(){ System.out.println("I am a vehicle!"); } } class Car implements Vehicle { public void print(){ Vehicle.super.print(); System.out.println("I am a car!"); } } class Boat implements Vehicle { }
  • 11. Bringing Java 8 to Android
  • 12. Jack New Android tool that transforms .java into .dex Introduced on Android N. Suports Lambda Functions and Method References on all Android versions. Suports Default Methods and Streams only for Android 24+ Does nto support java.time.*
  • 13. Streams: LightweightStreams Streams API implementation using Java 7 Collections. List<String> names = Stream.of(people) .filter(p -> p.age > 16) .map(p -> p.name) .collect(Collectors.toList()); Method Count: 719 (1.1.2)
  • 14. Streams: RxJava Observables are fundamentally different from Streams (push vs. pull), but similar functionality can be obtained using Observable.from(myCollection). List<String> names = Observable.from(people) .filter(p -> p.age > 16) .map(p -> p.name) .toList().toBlocking().first(); Method Count: 5492 (1.1.8)
  • 15. Streams // Stream people.stream() .filter(p -> p.age > 16) .map(p -> p.name) .collect(Collectors.toList()); // LightweightStreams Stream.of(people) .filter(p -> p.age > 16) .map(p -> p.name) .collect(Collectors.toList()); // RxJava Observable.from(people) .filter(p -> p.age > 16) .map(p -> p.name) .toList().toBlocking().first();
  • 16. java.time.*: ThreeTenABP optimized version for Android. Same API as java.time.*, making them interchangeable. Method Count: 3280 ThreeTenBP
  • 17. Retrolambda Transforms Java 8 code into code compatible with Java 5, 6 and 7. Operates during compile time. Full support to Lambdas, Try-With-Resources and Method References. Partial support to default methods.
  • 19. Resumo RL Jack RxJava LS TT Streams 24+ ✔ ✔ Default Methods Partial 24+ Lambda ✔ ✔ Method References ✔ ✔ Try-With-Resources ✔ ✔ java.time.* ✔ - RL: Retrolambda, LS: LightweightStreams, TT: ThreeTenABP
  • 20. References Jack Jack e Java 8 Retrolambda Lightweight Stream ThreeTenABP Estudo sobre API para date RxJava Retrolambda vs Jack