Top Banner
1
25

Smart Migration to JDK 8

Jan 26, 2015

Download

Technology

Key points of migrating to JDK 8 Language Features.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Smart Migration to JDK 8

1

Page 2: Smart Migration to JDK 8

2

Smart Migration

to JDK 8

Page 3: 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

Page 4: Smart Migration to JDK 8

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

Page 5: Smart Migration to JDK 8

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

Page 6: Smart Migration to JDK 8

6

Lambda

Remove boilerplate code of anonymous inner classes. Better syntax.

From Imperative to Functional, From Sequential to Parallel

Page 7: Smart Migration to JDK 8

7

Demo

How to Migrate to Lambdas

Page 8: Smart Migration to JDK 8

8

Functions

java.util.Function – defines new functional interfaces, in addition to EventHandler, etc.

From Imperative to Functional, From Sequential to Parallel

Page 9: Smart Migration to JDK 8

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.

Page 10: Smart Migration to JDK 8

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

Page 11: Smart Migration to JDK 8

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

Page 12: Smart Migration to JDK 8

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

Page 13: Smart Migration to JDK 8

13

Page 14: Smart Migration to JDK 8

14

Demo

How to Migrate to Streams

Page 15: Smart Migration to JDK 8

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

Page 16: Smart Migration to JDK 8

16

Demo

How to Migrate to Method References

Page 17: Smart Migration to JDK 8

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

Page 18: Smart Migration to JDK 8

18

Demo

Automation and Batch Processing

Page 19: Smart Migration to JDK 8

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

Page 20: Smart Migration to JDK 8

20

Demo

Getting Started

Page 21: Smart Migration to JDK 8

21

Community Feedback From Imperative to Functional, From Sequential to Parallel

Page 22: Smart Migration to JDK 8

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

Page 23: Smart Migration to JDK 8

23

Next Steps

Page 24: Smart Migration to JDK 8

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

Page 25: Smart Migration to JDK 8

25