SlideShare a Scribd company logo
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

PDF
Android antipatterns
PDF
Smart Migration to JDK 8
PPTX
모던자바의 역습
PPTX
Java 14 features
PDF
JDKs 10 to 14 (and beyond)
PDF
Intro to Java 8 Closures (Dainius Mezanskas)
PPTX
Functional java 8
PDF
Complete Java Course
Android antipatterns
Smart Migration to JDK 8
모던자바의 역습
Java 14 features
JDKs 10 to 14 (and beyond)
Intro to Java 8 Closures (Dainius Mezanskas)
Functional java 8
Complete Java Course

What's hot (20)

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

Viewers also liked (9)

PDF
Usando Java 8 no Android
PPTX
JDK 9: Big Changes To Make Java Smaller
PDF
I/O Extended (GDG Bogor) - Andrew Kurniadi
PDF
Java 8 and 9 in Anger
PDF
What's New in Java SE 9
PPTX
Java 9 Functionality and Tooling
PPTX
Modularization With Project Jigsaw in JDK 9
PDF
Java 9 – The Ultimate Feature List
PPTX
55 New Features in JDK 9
Usando Java 8 no Android
JDK 9: Big Changes To Make Java Smaller
I/O Extended (GDG Bogor) - Andrew Kurniadi
Java 8 and 9 in Anger
What's New in Java SE 9
Java 9 Functionality and Tooling
Modularization With Project Jigsaw in JDK 9
Java 9 – The Ultimate Feature List
55 New Features in JDK 9
Ad

Similar to Using Java 8 on Android (20)

PPTX
New Features of JAVA SE8
DOCX
Colloquium Report
PDF
Java 8 - Lambdas and much more
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
PPTX
java new technology
PPTX
New features in jdk8 iti
PPTX
Software Uni Conf October 2014
PPT
Javalecture 1
PPTX
brief introduction to core java programming.pptx
PPTX
New Features in JDK 8
PPTX
Java 9 features
PDF
Java features. Java 8, 9, 10, 11
PPTX
Java 7 & 8 New Features
PDF
Alive and Well with Java 8
PPT
whats new in java 8
PDF
Java 8-revealed
PPTX
DevNexus 2020: Discover Modern Java
PPTX
It's always your fault
PPTX
Chapter 2.1
PPT
Basic java part_ii
New Features of JAVA SE8
Colloquium Report
Java 8 - Lambdas and much more
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
java new technology
New features in jdk8 iti
Software Uni Conf October 2014
Javalecture 1
brief introduction to core java programming.pptx
New Features in JDK 8
Java 9 features
Java features. Java 8, 9, 10, 11
Java 7 & 8 New Features
Alive and Well with Java 8
whats new in java 8
Java 8-revealed
DevNexus 2020: Discover Modern Java
It's always your fault
Chapter 2.1
Basic java part_ii

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
A Presentation on Artificial Intelligence
PPTX
Cloud computing and distributed systems.
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Encapsulation theory and applications.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Review of recent advances in non-invasive hemoglobin estimation
Per capita expenditure prediction using model stacking based on satellite ima...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine Learning_overview_presentation.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Big Data Technologies - Introduction.pptx
Network Security Unit 5.pdf for BCA BBA.
A Presentation on Artificial Intelligence
Cloud computing and distributed systems.
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
A comparative analysis of optical character recognition models for extracting...
Machine learning based COVID-19 study performance prediction
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation_ Review paper, used for researhc scholars
Encapsulation theory and applications.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Assigned Numbers - 2025 - Bluetooth® Document

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