Date post: | 26-Jan-2015 |
Category: |
Technology |
Upload: | geertjan-wielenga |
View: | 110 times |
Download: | 1 times |
1
2
Smart Migration
to JDK 8
3
Language Changes in Java 8
From Imperative to Functional
– Use modern constructs other newer languages use.
– Enhance code readability.
– Strip out the boilerplate code.
– Stop telling the compiler what to do.
From Sequential to Parallel
– Execute code in parallel without extra effort.
– Divide problems into subproblems
solve subproblems simultaneously
and combine the results.
– Aggregate operations instead of fork/join.
Themes
4
Language Changes in Java 8
Virtual extension methods
Functional interfaces
Lambdas
Method references
Streams
Collections
Time
Concurrency
IO/NIO
Reflections and annotations
Miscellaneous
From Imperative to Functional, From Sequential to Parallel
5
Building Blocks of Java 8
Virtual extension methods
– Break existing code when adding a method to an interface?
– Java 8 allows to add new methods to interfaces.
– Revolutionary – interface evolution is now possible.
Functional interfaces
– A functional interface
defines exactly one abstract method,
focused on one specific task.
– For example, java.lang.Runnable
defines one abstract method “run()”.
– @FunctionalInterface
From Imperative to Functional, From Sequential to Parallel
6
Lambda
Remove boilerplate code of anonymous inner classes. Better syntax.
From Imperative to Functional, From Sequential to Parallel
7
Demo
How to Migrate to Lambdas
8
Functions
java.util.Function – defines new functional interfaces, in addition to EventHandler, etc.
From Imperative to Functional, From Sequential to Parallel
9
Functions
java.util.Function – defines new functional interfaces, in addition to EventHandler, etc.
From Imperative to Functional, From Sequential to Parallel
New Functional
Interfaces
Many Primitive Specializations
Function<T,R> BiFunction, ToIntFunction, LongToIntFunction, etc.
Predicate<T> IntPredicate, LongPredicate, DoublePredicate, etc.
Consumer<T> IntConsumer, DoubleConsumer, LongConsumer, etc.
Supplier<T> IntSupplier, BinarySupplier, DoubleSupplier, etc.
BinaryOperator<T> IntBinaryOperator, IntUnaryOperator, etc.
10
Streams: Pipes & Filters
java.util.Stream – provides a pipeline with filters. Source is processed through a pipe.
Each successive aggregate operation in the pipeline is one of:
– Intermediate – map, filter, sorted, distinct, limit, skip, substream
– Terminal – forEach, sum, collect, reduce, count, findFirst, findAny
Lazy evaluation – process only as needed and as many times as needed, e.g., “findFirst”.
No intermediate storage.
From Imperative to Functional, From Sequential to Parallel
11
Streams: Classes & Methods
From Imperative to Functional, From Sequential to Parallel
allMatch
anyMatch
builder
collect
concat
count
distinct
empty
filter
findFirst
findAny
flatMap forEach
forEachOrder
generate
iterate limit map
max min
noneMatch
of peek reduce skip sorted
toArray
IntStream
DoubleStream
LongStream Builder
range
sequential
boxed
parallel
12
Streams: Collections
Distinguish between “what should be done” and “how it should be done”.
Collections APIs have been extended. Collections can be exposed as Streams.
Identify and refactor “for” loops. Replace with internal iterations provided by Streams.
That lets you express concurrent processing of elements in parallel.
From Imperative to Functional, From Sequential to Parallel
13
14
Demo
How to Migrate to Streams
15
Method Reference
Method references are an abbreviated style for expressing lambdas.
x -> String.valueOf(x) / String::valueOf
x -> x.toString / Object::toString
() -> x.toString() / x::toString
() -> new ArrayList() / ArrayList::new
Where a method already exists to perform an operation,
a method reference can be used instead of a lambda.
From Imperative to Functional, From Sequential to Parallel
16
Demo
How to Migrate to Method References
17
Automation and Batch Processing
Refactoring tool scans across a scope – all projects, single project, single package, file...
Find candidates for upgrading to lambdas, functional operations, and method references.
From Imperative to Functional, From Sequential to Parallel
18
Demo
Automation and Batch Processing
19
Getting Started
Download NetBeans IDE 8.
Register JDK 8 in the IDE.
Set JDK 8 on the project.
Change Source Format to JDK 8.
From Imperative to Functional, From Sequential to Parallel
20
Demo
Getting Started
21
Community Feedback From Imperative to Functional, From Sequential to Parallel
22
Summary
Virtual extension methods and functional interfaces make lambdas possible.
Lambdas remove boilerplate code of anonymous inner classes.
Streams provide pipelines, filters, and parallelism to Collections.
Method references are an alternative shorthand for lambdas.
From Imperative to Functional, From Sequential to Parallel
23
Next Steps
24
References
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html
http://blog.hartveld.com/2013/03/jdk-8-13-interface-default-method.html
http://www.techempower.com/blog/2013/03/26/everything-about-java-8/
http://technology.amis.nl/2013/10/05/java-8-collection-enhancements-leveraging-lambda-expressions-or-how-java-
emulates-sql/
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html
http://drunkendev.blogspot.nl/2014/01/migrating-to-jdk-8-with-netbeans.html
http://devoxx.com/display/DV12/Closures+and+Collections+-+the+World+After+Eight
http://refactoring.info/tools/LambdaFicator/
From Imperative to Functional, From Sequential to Parallel
25