SlideShare a Scribd company logo
1 of 78
Download to read offline
Java 8 to the rescue!?
JavaZone 2013
1
torsdag 12. september 13
2
RETURNOF
THE AVA
J
JAVA EIGHT
torsdag 12. september 13
Fredrik Vraalsen
vraalsen@iterate.no
Dad,
Java developer,
Scala enthusiast,
sci-fi fan, photo geek
and Age of Conan
assassin
@fredriv
© Fredrik Vraalsen 2008
torsdag 12. september 13
Java 8 – what’s new?
Lambdas, Extension Methods, SAMs, Streams, Method Handles, Oh My!
Nashorn JS engine
JSR-310: Date & time API
No more PermGen– where classloaders go to die
Compact profiles
...
4
torsdag 12. september 13
Java 8 – what’s new?
Lambdas, Extension Methods, SAMs, Streams, Method Handles, Oh My!
Nashorn JS engine
JSR-310: Date & time API
No more PermGen– where classloaders go to die
Compact profiles
...
5
torsdag 12. september 13
Your Mission
Should you choose to accept it
Sort a list of people
by their names
torsdag 12. september 13
Java 7
List<Person> people = ...
Collections.sort(people);
7
torsdag 12. september 13
Java 7
Collections.sort(people, new Comparator<Person>() {
});
8
torsdag 12. september 13
Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
}
});
9
torsdag 12. september 13
Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
});
10
torsdag 12. september 13
We are not amused
© Fredrik Vraalsen 2012
torsdag 12. september 13
Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
});
12
torsdag 12. september 13
Java 7
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
Collections.sort(people, byName);
13
torsdag 12. september 13
Java 7
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
sort(people, byName);Collections.
14
torsdag 12. september 13
sort(people, byName)
Java 7
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
( , ;
Lisp?
15
torsdag 12. september 13
What about Java 8?
© Fredrik Vraalsen 2012
torsdag 12. september 13
Java 8
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
sort(people, byName);
17
torsdag 12. september 13
Java 8
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
people.sort(byName);
18
torsdag 12. september 13
Java 8
19
java.util.List:
default void sort(Comparator<? super E> c)
torsdag 12. september 13
Java 8
Defender Method
Default Method
(Virtual) Extension Method
20
java.util.List:
default void sort(Comparator<? super E> c)
torsdag 12. september 13
Java 8
java.util.List:
default void sort(Comparator<? super E> c) {
}
Defender Method
Default Method
(Virtual) Extension Method
21
torsdag 12. september 13
Java 8
java.util.List:
default void sort(Comparator<? super E> c) {
Collections.sort(this, c);
}
Defender Method
Default Method
(Virtual) Extension Method
22
torsdag 12. september 13
Extension method
Extend interfaces with new methods
Compatibility
Default implementation
Override
23
torsdag 12. september 13
C# extension methods
Static methods
Cannot be overridden
One-size fits all solution
24
torsdag 12. september 13
Scala Traits
“Interfaces on steroids”
Method implementations
Variables
Everything a class can have – except constructors
Can mix in multiple traits
25
torsdag 12. september 13
Ok, nice...
What else?
© Fredrik Vraalsen 2012
torsdag 12. september 13
Java 8
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
Single Abstract Method
(SAM)
27
torsdag 12. september 13
Single Abstract Method
Interface with only one (non-default) method
Abstract class with only one abstract method
Sound familiar?
Pretty much every event listener, callback mechanism, ...
Can be replaced by a Lambda
28
torsdag 12. september 13
Lambda
Closure
Anonymous function
29
torsdag 12. september 13
SAM
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
30
torsdag 12. september 13
SAM
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
BodyReturn type
Parameter list
Method name
Type
31
torsdag 12. september 13
Lambda
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
};
BodyReturn type
Parameter list
Method name
Type
32
torsdag 12. september 13
Lambda
Comparator<Person> byName =
return x.getName().compareTo(y.getName());
};
BodyReturn type?
Parameter list
(Person x, Person y) -> {
33
torsdag 12. september 13
Lambda
Comparator<Person> byName =
(Person x, Person y) ->
Parameter list
BodyReturn type?
x.getName().compareTo(y.getName());
{
};
return
34
torsdag 12. september 13
Lambda
Comparator<Person> byName =
Parameter list
BodyReturn type?
(x, y) -> x.getName().compareTo(y.getName());(Person x, Person y)
35
torsdag 12. september 13
Lambda – putting it together
Comparator<Person> byName =
(x, y) -> x.getName().compareTo(y.getName());
people.sort(byName);
36
torsdag 12. september 13
Lambda – putting it together
people.sort((x, y) -> x.getName().compareTo(y.getName()));
37
torsdag 12. september 13
Lambda vs Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
});
38
torsdag 12. september 13
Lambda vs Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().compareTo(y.getName());
}
});
vs
people.sort((x, y) -> x.getName().compareTo(y.getName()));
39
torsdag 12. september 13
Cool!
Now onwards... © Fredrik Vraalsen 2013
torsdag 12. september 13
Method handles
Reference to methods
Can be used in place of lambdas
41
torsdag 12. september 13
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
42
torsdag 12. september 13
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparator<Person> byName =
(Person a, Person b) -> a.compareByName(b);
43
torsdag 12. september 13
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparator<Person> byName =
(a, b) -> a.compareByName(b);
44
torsdag 12. september 13
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparator<Person> byName = Person::compareByName;
45
torsdag 12. september 13
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparator<Person> byName = Person::compareByName;
people.sort(byName);
46
torsdag 12. september 13
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
people.sort(Person::compareByName);
47
torsdag 12. september 13
So far, so good?
Extension methods
Lambdas
Method handles
48
torsdag 12. september 13
So, what’s the catch?© Fredrik Vraalsen 2012
torsdag 12. september 13
What’s wrong with using List::sort ?
Modifies existing collection
Others may be using it?
Concurrency issues
Performance
50
torsdag 12. september 13
Streams
© Fredrik Vraalsen 2008
torsdag 12. september 13
The old fashioned way
List<String> namesOfAdults = new ArrayList<>();
for (Person p : people) {
if (p.getAge() >= 18) {
namesOfAdults.add(p.getName());
}
}
52
torsdag 12. september 13
Streams
Stream<Person> stream = people.stream();
Stream<Person> adults = stream.filter(p -> p.getAge() >= 18);
Stream<String> namesOfAdults =
adults.map(Person::getName);
53
torsdag 12. september 13
Streams – compose
Stream<String> namesOfAdults = people.stream().
filter(p -> p.getAge() >= 18).
map(Person::getName);
54
torsdag 12. september 13
Streams – collect
List<String> namesOfAdults = people.stream().
filter(p -> p.getAge() >= 18).
map(Person::getName).
collect(Collectors.toList());
55
torsdag 12. september 13
Streams – collect
List<String> namesOfAdults = people.stream().
filter(p -> p.getAge() >= 18).
map(Person::getName).
collect(toList());
56
torsdag 12. september 13
Performance
© Fredrik Vraalsen 2012
torsdag 12. september 13
Streams – performance
List<String> namesOfAdults = people.stream().
filter(p -> p.getAge() >= 18).
map(Person::getName).
collect(toList());
58
torsdag 12. september 13
Streams – performance
List<String> namesOfAdults = people.parallelStream().
filter(p -> p.getAge() >= 18).
map(Person::getName).
collect(toList());
59
torsdag 12. september 13
Scala FTW!
val namesOfAdults = people.
filter(p => p.age >= 18).
map(p => p.name)
60
torsdag 12. september 13
Scala FTW!
val namesOfAdults = people.
filter(_.age >= 18).
map(_.name)
61
torsdag 12. september 13
Scala FTW!
val namesOfAdults = people.filter(_.age >= 18).map(_.name)
62
torsdag 12. september 13
Scala FTW!
val namesOfAdults = people.par.filter(_.age >= 18).map(_.name).toList
63
torsdag 12. september 13
Streams – sorting
List<Person> sorted = people.stream().
sort(Person::compareByName).
collect(Collectors.toList());
64
torsdag 12. september 13
Scala FTW 2!
val sorted = people.sortBy(_.name)
65
torsdag 12. september 13
Streams – pipelines
Source
collection, array, generator function, IO channel, ...
Intermediate operations (Stream-producing)
filter, map, ...
Terminal operations (value-producing)
66
torsdag 12. september 13
Streams
java.util.stream
Create new results
Lazy
Parallelizable
67
torsdag 12. september 13
Ready to
make the jump!?
© Fredrik Vraalsen 2013
torsdag 12. september 13
Play with it!
Download from http://jdk8.java.net/download.html
Whitepapers at http://openjdk.java.net/projects/lambda/
FAQ http://www.lambdafaq.org/
Supported in IntelliJ IDEA 12 (and NetBeans nightlies?)
69
torsdag 12. september 13
Why use X instead?
Java 8 not yet released (duh!)
March 2014?
Will be a long time before you can use it in enterprise dev!
Clojure and Scala available NOW! (JDK 1.5 and 1.6)
Important things are still missing
70
torsdag 12. september 13
What’s missing?
© Fredrik Vraalsen 2012
torsdag 12. september 13
What’s missing?
Immutability
Value types
Immutable data structures
Lists, maps, sets, etc.
72
torsdag 12. september 13
What’s the big deal?
Functional programming is all about values!
And transformations (functions) computing new values
Concurrency
Parallelism
Robustness
Testability
Better / higher abstractions
Less code, less bugs!
73
torsdag 12. september 13
Some help to be found
Immutable collections
Google Guava, FunctionalJava
Concurrency mechanisms
Akka (Actors, STM)
74
torsdag 12. september 13
What else is missing?
Type inference
Pattern matching
For comprehensions
Named and default parameters
Properties
Implicit parameters / conversions
and much more...
http://stackoverflow.com/questions/3844745/scala-advantages-after-java-having-closures/3844844#3844844 75
torsdag 12. september 13
LambdaJ
Easier manipulation of collections and more, limited closure support
Google Guava
Lots of nice utility classes, some functional programming support
FunctionalJava
Most “pure” FP – but slow, separate collection hierarchy
Funcito
Simplify creation of functions from existing methods
Integrates with Guava, FunctionalJava, Jedi, ...
But I’m stuck in Java 6... Help!
76
torsdag 12. september 13
Questions?
© Fredrik Vraalsen 2012
vraalsen@iterate.no / @fredriv
torsdag 12. september 13
torsdag 12. september 13

More Related Content

What's hot

Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCOUM SAOKOSAL
 
There's a Prolog in your Scala!
There's a Prolog in your Scala!There's a Prolog in your Scala!
There's a Prolog in your Scala!George Leontiev
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Codemotion
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionOUM SAOKOSAL
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritanceshatha00
 
Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016Holden Karau
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Sanjeev_Knoldus
 
Java → kotlin: Tests Made Simple
Java → kotlin: Tests Made SimpleJava → kotlin: Tests Made Simple
Java → kotlin: Tests Made Simpleleonsabr
 
Introduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at lastIntroduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at lastHolden Karau
 
Dart London hackathon
Dart  London hackathonDart  London hackathon
Dart London hackathonchrisbuckett
 

What's hot (12)

Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
There's a Prolog in your Scala!
There's a Prolog in your Scala!There's a Prolog in your Scala!
There's a Prolog in your Scala!
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - Collection
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
 
Java → kotlin: Tests Made Simple
Java → kotlin: Tests Made SimpleJava → kotlin: Tests Made Simple
Java → kotlin: Tests Made Simple
 
Introduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at lastIntroduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at last
 
Dart London hackathon
Dart  London hackathonDart  London hackathon
Dart London hackathon
 

Similar to Java 8 to the rescue!?

JavaOne 2013 - Clojure for Java Developers
JavaOne 2013 - Clojure for Java DevelopersJavaOne 2013 - Clojure for Java Developers
JavaOne 2013 - Clojure for Java DevelopersJan Kronquist
 
Java Extension Methods
Java Extension MethodsJava Extension Methods
Java Extension MethodsAndreas Enbohm
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented ProgrammingWeizhong Yang
 
FUNctional Programming in Java 8
FUNctional Programming in Java 8FUNctional Programming in Java 8
FUNctional Programming in Java 8Richard Walker
 
First Ride on Rust
First Ride on RustFirst Ride on Rust
First Ride on RustDavid Evans
 
Introduction to Twig
Introduction to TwigIntroduction to Twig
Introduction to Twigmarkstory
 
Baking Delicious Modularity in Scala
Baking Delicious Modularity in ScalaBaking Delicious Modularity in Scala
Baking Delicious Modularity in ScalaDerek Wyatt
 
Securing Client Side Data
 Securing Client Side Data Securing Client Side Data
Securing Client Side DataGrgur Grisogono
 
CDI do básico ao avançado
CDI do básico ao avançadoCDI do básico ao avançado
CDI do básico ao avançadoAlberto Souza
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Chris Richardson
 
Deconstructing Functional Programming
Deconstructing Functional ProgrammingDeconstructing Functional Programming
Deconstructing Functional ProgrammingC4Media
 
Comparing GWT Transport Mechanisms
Comparing GWT Transport MechanismsComparing GWT Transport Mechanisms
Comparing GWT Transport Mechanismslastrand
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulationIntro C# Book
 

Similar to Java 8 to the rescue!? (18)

JavaOne 2013 - Clojure for Java Developers
JavaOne 2013 - Clojure for Java DevelopersJavaOne 2013 - Clojure for Java Developers
JavaOne 2013 - Clojure for Java Developers
 
StORM preview
StORM previewStORM preview
StORM preview
 
Mongo db for C# Developers
Mongo db for C# DevelopersMongo db for C# Developers
Mongo db for C# Developers
 
Java Extension Methods
Java Extension MethodsJava Extension Methods
Java Extension Methods
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
 
FUNctional Programming in Java 8
FUNctional Programming in Java 8FUNctional Programming in Java 8
FUNctional Programming in Java 8
 
PHP 5 Boot Camp
PHP 5 Boot CampPHP 5 Boot Camp
PHP 5 Boot Camp
 
First Ride on Rust
First Ride on RustFirst Ride on Rust
First Ride on Rust
 
Introduction to Twig
Introduction to TwigIntroduction to Twig
Introduction to Twig
 
Baking Delicious Modularity in Scala
Baking Delicious Modularity in ScalaBaking Delicious Modularity in Scala
Baking Delicious Modularity in Scala
 
Securing Client Side Data
 Securing Client Side Data Securing Client Side Data
Securing Client Side Data
 
CDI do básico ao avançado
CDI do básico ao avançadoCDI do básico ao avançado
CDI do básico ao avançado
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
 
Backbone
BackboneBackbone
Backbone
 
Deconstructing Functional Programming
Deconstructing Functional ProgrammingDeconstructing Functional Programming
Deconstructing Functional Programming
 
Invitation to Scala
Invitation to ScalaInvitation to Scala
Invitation to Scala
 
Comparing GWT Transport Mechanisms
Comparing GWT Transport MechanismsComparing GWT Transport Mechanisms
Comparing GWT Transport Mechanisms
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
 

More from Fredrik Vraalsen

Building applications with Serverless Framework and AWS Lambda - JavaZone 2019
Building applications with Serverless Framework and AWS Lambda - JavaZone 2019Building applications with Serverless Framework and AWS Lambda - JavaZone 2019
Building applications with Serverless Framework and AWS Lambda - JavaZone 2019Fredrik Vraalsen
 
Building applications with Serverless Framework and AWS Lambda
Building applications with Serverless Framework and AWS LambdaBuilding applications with Serverless Framework and AWS Lambda
Building applications with Serverless Framework and AWS LambdaFredrik Vraalsen
 
Kafka and Kafka Streams in the Global Schibsted Data Platform
Kafka and Kafka Streams in the Global Schibsted Data PlatformKafka and Kafka Streams in the Global Schibsted Data Platform
Kafka and Kafka Streams in the Global Schibsted Data PlatformFredrik Vraalsen
 
Event stream processing using Kafka streams
Event stream processing using Kafka streamsEvent stream processing using Kafka streams
Event stream processing using Kafka streamsFredrik Vraalsen
 
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Java 8 DOs and DON'Ts - javaBin Oslo May 2015Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Java 8 DOs and DON'Ts - javaBin Oslo May 2015Fredrik Vraalsen
 
Git i praksis - erfaringer med overgang fra ClearCase til Git
Git i praksis - erfaringer med overgang fra ClearCase til GitGit i praksis - erfaringer med overgang fra ClearCase til Git
Git i praksis - erfaringer med overgang fra ClearCase til GitFredrik Vraalsen
 

More from Fredrik Vraalsen (7)

Building applications with Serverless Framework and AWS Lambda - JavaZone 2019
Building applications with Serverless Framework and AWS Lambda - JavaZone 2019Building applications with Serverless Framework and AWS Lambda - JavaZone 2019
Building applications with Serverless Framework and AWS Lambda - JavaZone 2019
 
Building applications with Serverless Framework and AWS Lambda
Building applications with Serverless Framework and AWS LambdaBuilding applications with Serverless Framework and AWS Lambda
Building applications with Serverless Framework and AWS Lambda
 
Kafka and Kafka Streams in the Global Schibsted Data Platform
Kafka and Kafka Streams in the Global Schibsted Data PlatformKafka and Kafka Streams in the Global Schibsted Data Platform
Kafka and Kafka Streams in the Global Schibsted Data Platform
 
Scala intro workshop
Scala intro workshopScala intro workshop
Scala intro workshop
 
Event stream processing using Kafka streams
Event stream processing using Kafka streamsEvent stream processing using Kafka streams
Event stream processing using Kafka streams
 
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Java 8 DOs and DON'Ts - javaBin Oslo May 2015Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
 
Git i praksis - erfaringer med overgang fra ClearCase til Git
Git i praksis - erfaringer med overgang fra ClearCase til GitGit i praksis - erfaringer med overgang fra ClearCase til Git
Git i praksis - erfaringer med overgang fra ClearCase til Git
 

Recently uploaded

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 

Recently uploaded (20)

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 

Java 8 to the rescue!?

  • 1. Java 8 to the rescue!? JavaZone 2013 1 torsdag 12. september 13
  • 3. Fredrik Vraalsen vraalsen@iterate.no Dad, Java developer, Scala enthusiast, sci-fi fan, photo geek and Age of Conan assassin @fredriv © Fredrik Vraalsen 2008 torsdag 12. september 13
  • 4. Java 8 – what’s new? Lambdas, Extension Methods, SAMs, Streams, Method Handles, Oh My! Nashorn JS engine JSR-310: Date & time API No more PermGen– where classloaders go to die Compact profiles ... 4 torsdag 12. september 13
  • 5. Java 8 – what’s new? Lambdas, Extension Methods, SAMs, Streams, Method Handles, Oh My! Nashorn JS engine JSR-310: Date & time API No more PermGen– where classloaders go to die Compact profiles ... 5 torsdag 12. september 13
  • 6. Your Mission Should you choose to accept it Sort a list of people by their names torsdag 12. september 13
  • 7. Java 7 List<Person> people = ... Collections.sort(people); 7 torsdag 12. september 13
  • 8. Java 7 Collections.sort(people, new Comparator<Person>() { }); 8 torsdag 12. september 13
  • 9. Java 7 Collections.sort(people, new Comparator<Person>() { public int compare(Person x, Person y) { } }); 9 torsdag 12. september 13
  • 10. Java 7 Collections.sort(people, new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }); 10 torsdag 12. september 13
  • 11. We are not amused © Fredrik Vraalsen 2012 torsdag 12. september 13
  • 12. Java 7 Collections.sort(people, new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }); 12 torsdag 12. september 13
  • 13. Java 7 Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }; Collections.sort(people, byName); 13 torsdag 12. september 13
  • 14. Java 7 Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }; sort(people, byName);Collections. 14 torsdag 12. september 13
  • 15. sort(people, byName) Java 7 Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }; ( , ; Lisp? 15 torsdag 12. september 13
  • 16. What about Java 8? © Fredrik Vraalsen 2012 torsdag 12. september 13
  • 17. Java 8 Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }; sort(people, byName); 17 torsdag 12. september 13
  • 18. Java 8 Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }; people.sort(byName); 18 torsdag 12. september 13
  • 19. Java 8 19 java.util.List: default void sort(Comparator<? super E> c) torsdag 12. september 13
  • 20. Java 8 Defender Method Default Method (Virtual) Extension Method 20 java.util.List: default void sort(Comparator<? super E> c) torsdag 12. september 13
  • 21. Java 8 java.util.List: default void sort(Comparator<? super E> c) { } Defender Method Default Method (Virtual) Extension Method 21 torsdag 12. september 13
  • 22. Java 8 java.util.List: default void sort(Comparator<? super E> c) { Collections.sort(this, c); } Defender Method Default Method (Virtual) Extension Method 22 torsdag 12. september 13
  • 23. Extension method Extend interfaces with new methods Compatibility Default implementation Override 23 torsdag 12. september 13
  • 24. C# extension methods Static methods Cannot be overridden One-size fits all solution 24 torsdag 12. september 13
  • 25. Scala Traits “Interfaces on steroids” Method implementations Variables Everything a class can have – except constructors Can mix in multiple traits 25 torsdag 12. september 13
  • 26. Ok, nice... What else? © Fredrik Vraalsen 2012 torsdag 12. september 13
  • 27. Java 8 Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }; Single Abstract Method (SAM) 27 torsdag 12. september 13
  • 28. Single Abstract Method Interface with only one (non-default) method Abstract class with only one abstract method Sound familiar? Pretty much every event listener, callback mechanism, ... Can be replaced by a Lambda 28 torsdag 12. september 13
  • 30. SAM Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }; 30 torsdag 12. september 13
  • 31. SAM Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }; BodyReturn type Parameter list Method name Type 31 torsdag 12. september 13
  • 32. Lambda Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }; BodyReturn type Parameter list Method name Type 32 torsdag 12. september 13
  • 33. Lambda Comparator<Person> byName = return x.getName().compareTo(y.getName()); }; BodyReturn type? Parameter list (Person x, Person y) -> { 33 torsdag 12. september 13
  • 34. Lambda Comparator<Person> byName = (Person x, Person y) -> Parameter list BodyReturn type? x.getName().compareTo(y.getName()); { }; return 34 torsdag 12. september 13
  • 35. Lambda Comparator<Person> byName = Parameter list BodyReturn type? (x, y) -> x.getName().compareTo(y.getName());(Person x, Person y) 35 torsdag 12. september 13
  • 36. Lambda – putting it together Comparator<Person> byName = (x, y) -> x.getName().compareTo(y.getName()); people.sort(byName); 36 torsdag 12. september 13
  • 37. Lambda – putting it together people.sort((x, y) -> x.getName().compareTo(y.getName())); 37 torsdag 12. september 13
  • 38. Lambda vs Java 7 Collections.sort(people, new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }); 38 torsdag 12. september 13
  • 39. Lambda vs Java 7 Collections.sort(people, new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }); vs people.sort((x, y) -> x.getName().compareTo(y.getName())); 39 torsdag 12. september 13
  • 40. Cool! Now onwards... © Fredrik Vraalsen 2013 torsdag 12. september 13
  • 41. Method handles Reference to methods Can be used in place of lambdas 41 torsdag 12. september 13
  • 42. Method handles class Person { ... public int compareByName(Person other) { return name.compareTo(other.name); } } 42 torsdag 12. september 13
  • 43. Method handles class Person { ... public int compareByName(Person other) { return name.compareTo(other.name); } } Comparator<Person> byName = (Person a, Person b) -> a.compareByName(b); 43 torsdag 12. september 13
  • 44. Method handles class Person { ... public int compareByName(Person other) { return name.compareTo(other.name); } } Comparator<Person> byName = (a, b) -> a.compareByName(b); 44 torsdag 12. september 13
  • 45. Method handles class Person { ... public int compareByName(Person other) { return name.compareTo(other.name); } } Comparator<Person> byName = Person::compareByName; 45 torsdag 12. september 13
  • 46. Method handles class Person { ... public int compareByName(Person other) { return name.compareTo(other.name); } } Comparator<Person> byName = Person::compareByName; people.sort(byName); 46 torsdag 12. september 13
  • 47. Method handles class Person { ... public int compareByName(Person other) { return name.compareTo(other.name); } } people.sort(Person::compareByName); 47 torsdag 12. september 13
  • 48. So far, so good? Extension methods Lambdas Method handles 48 torsdag 12. september 13
  • 49. So, what’s the catch?© Fredrik Vraalsen 2012 torsdag 12. september 13
  • 50. What’s wrong with using List::sort ? Modifies existing collection Others may be using it? Concurrency issues Performance 50 torsdag 12. september 13
  • 51. Streams © Fredrik Vraalsen 2008 torsdag 12. september 13
  • 52. The old fashioned way List<String> namesOfAdults = new ArrayList<>(); for (Person p : people) { if (p.getAge() >= 18) { namesOfAdults.add(p.getName()); } } 52 torsdag 12. september 13
  • 53. Streams Stream<Person> stream = people.stream(); Stream<Person> adults = stream.filter(p -> p.getAge() >= 18); Stream<String> namesOfAdults = adults.map(Person::getName); 53 torsdag 12. september 13
  • 54. Streams – compose Stream<String> namesOfAdults = people.stream(). filter(p -> p.getAge() >= 18). map(Person::getName); 54 torsdag 12. september 13
  • 55. Streams – collect List<String> namesOfAdults = people.stream(). filter(p -> p.getAge() >= 18). map(Person::getName). collect(Collectors.toList()); 55 torsdag 12. september 13
  • 56. Streams – collect List<String> namesOfAdults = people.stream(). filter(p -> p.getAge() >= 18). map(Person::getName). collect(toList()); 56 torsdag 12. september 13
  • 57. Performance © Fredrik Vraalsen 2012 torsdag 12. september 13
  • 58. Streams – performance List<String> namesOfAdults = people.stream(). filter(p -> p.getAge() >= 18). map(Person::getName). collect(toList()); 58 torsdag 12. september 13
  • 59. Streams – performance List<String> namesOfAdults = people.parallelStream(). filter(p -> p.getAge() >= 18). map(Person::getName). collect(toList()); 59 torsdag 12. september 13
  • 60. Scala FTW! val namesOfAdults = people. filter(p => p.age >= 18). map(p => p.name) 60 torsdag 12. september 13
  • 61. Scala FTW! val namesOfAdults = people. filter(_.age >= 18). map(_.name) 61 torsdag 12. september 13
  • 62. Scala FTW! val namesOfAdults = people.filter(_.age >= 18).map(_.name) 62 torsdag 12. september 13
  • 63. Scala FTW! val namesOfAdults = people.par.filter(_.age >= 18).map(_.name).toList 63 torsdag 12. september 13
  • 64. Streams – sorting List<Person> sorted = people.stream(). sort(Person::compareByName). collect(Collectors.toList()); 64 torsdag 12. september 13
  • 65. Scala FTW 2! val sorted = people.sortBy(_.name) 65 torsdag 12. september 13
  • 66. Streams – pipelines Source collection, array, generator function, IO channel, ... Intermediate operations (Stream-producing) filter, map, ... Terminal operations (value-producing) 66 torsdag 12. september 13
  • 68. Ready to make the jump!? © Fredrik Vraalsen 2013 torsdag 12. september 13
  • 69. Play with it! Download from http://jdk8.java.net/download.html Whitepapers at http://openjdk.java.net/projects/lambda/ FAQ http://www.lambdafaq.org/ Supported in IntelliJ IDEA 12 (and NetBeans nightlies?) 69 torsdag 12. september 13
  • 70. Why use X instead? Java 8 not yet released (duh!) March 2014? Will be a long time before you can use it in enterprise dev! Clojure and Scala available NOW! (JDK 1.5 and 1.6) Important things are still missing 70 torsdag 12. september 13
  • 71. What’s missing? © Fredrik Vraalsen 2012 torsdag 12. september 13
  • 72. What’s missing? Immutability Value types Immutable data structures Lists, maps, sets, etc. 72 torsdag 12. september 13
  • 73. What’s the big deal? Functional programming is all about values! And transformations (functions) computing new values Concurrency Parallelism Robustness Testability Better / higher abstractions Less code, less bugs! 73 torsdag 12. september 13
  • 74. Some help to be found Immutable collections Google Guava, FunctionalJava Concurrency mechanisms Akka (Actors, STM) 74 torsdag 12. september 13
  • 75. What else is missing? Type inference Pattern matching For comprehensions Named and default parameters Properties Implicit parameters / conversions and much more... http://stackoverflow.com/questions/3844745/scala-advantages-after-java-having-closures/3844844#3844844 75 torsdag 12. september 13
  • 76. LambdaJ Easier manipulation of collections and more, limited closure support Google Guava Lots of nice utility classes, some functional programming support FunctionalJava Most “pure” FP – but slow, separate collection hierarchy Funcito Simplify creation of functions from existing methods Integrates with Guava, FunctionalJava, Jedi, ... But I’m stuck in Java 6... Help! 76 torsdag 12. september 13
  • 77. Questions? © Fredrik Vraalsen 2012 vraalsen@iterate.no / @fredriv torsdag 12. september 13