60
1 RETURN OF THE AVA J JAVA EIGHT

Java 8 - Return of the Java

Embed Size (px)

DESCRIPTION

Introduction to new features in Java 8 and their use in functional programming, such as lambda methods, extension methods, method handles, the new Stream API, parallellism and laziness.

Citation preview

Page 1: Java 8 - Return of the Java

!1

RETURN OF THE AVAJ

JAVA EIGHT

Page 2: Java 8 - Return of the Java

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

...!2

Page 3: Java 8 - Return of the Java

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

...!3

Page 4: Java 8 - Return of the Java

Your Mission Should you choose to accept it

Sort a list of people by their names

Page 5: Java 8 - Return of the Java

Java 7Collections.sort(people, new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } });

!5

Page 6: Java 8 - Return of the Java

We are not amused

© Fredrik Vraalsen 2012

Page 7: Java 8 - Return of the Java

Java 8Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } };

Single Abstract Method !

(SAM)!7

Page 8: Java 8 - Return of the Java

Single Abstract MethodInterface 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

!8

Page 9: Java 8 - Return of the Java

Lambda

Closure

Anonymous function!9

Page 10: Java 8 - Return of the Java

SAMComparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } };

!10

Page 11: Java 8 - Return of the Java

SAMComparator<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

!11

Page 12: Java 8 - Return of the Java

LambdaComparator<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

!12

Page 13: Java 8 - Return of the Java

LambdaComparator<Person> byName = ! return x.getName().compareTo(y.getName()); };

BodyReturn type?

Parameter list

(Person x, Person y) -> {

!13

Page 14: Java 8 - Return of the Java

LambdaComparator<Person> byName = (Person x, Person y) ->

Parameter list

BodyReturn type?

x.getName().compareTo(y.getName());{

};return

!14

Page 15: Java 8 - Return of the Java

LambdaComparator<Person> byName =

Parameter list

BodyReturn type?

(x, y) -> x.getName().compareTo(y.getName()); (Person x, Person y)

!15

Page 16: Java 8 - Return of the Java

LambdaComparator<Person> byName = (x, y) -> x.getName().compareTo(y.getName()); !Collections.sort(people, byName);

!16

Page 17: Java 8 - Return of the Java

LambdaComparator<Person> byName = (x, y) -> x.getName().compareTo(y.getName()); !sort(people, byName);

!17

Page 18: Java 8 - Return of the Java

Lambda!!!sort(people, (x, y) -> x.getName().compareTo(y.getName()));

!18

Page 19: Java 8 - Return of the Java

Comparatorsimport static java.util.Comparator.comparing; … !sort(people, comparing((Person p) -> p.getName());

!19

Page 20: Java 8 - Return of the Java

Cool! !

!

!

!

Now onwards...© Fredrik Vraalsen 2013

Page 21: Java 8 - Return of the Java

Method handlesReference to methods

Can be used in place of lambdas

!21

Page 22: Java 8 - Return of the Java

Method handles!!!sort(people, comparing((Person p) -> p.getName()));

!22

Page 23: Java 8 - Return of the Java

Method handles!!!sort(people, comparing(Person::getName));

!23

Page 24: Java 8 - Return of the Java

Ok, nice... What else?

© Fredrik Vraalsen 2012

Page 25: Java 8 - Return of the Java

Extension methods!!!sort(people, comparing(Person::getName));

!25

Page 26: Java 8 - Return of the Java

Extension methods!!!people.sort(comparing(Person::getName));

!26

Page 27: Java 8 - Return of the Java

Extension methods

!27

java.util.List: ! default void sort(Comparator<? super E> c)

Page 28: Java 8 - Return of the Java

Extension methods

Defender Method Default Method

(Virtual) Extension Method!28

java.util.List: ! default void sort(Comparator<? super E> c)

Page 29: Java 8 - Return of the Java

Extension methodsjava.util.List: ! default void sort(Comparator<? super E> c) { } !

Defender Method Default Method

(Virtual) Extension Method!29

Page 30: Java 8 - Return of the Java

Extension methodsjava.util.List: ! default void sort(Comparator<? super E> c) { Collections.sort(this, c); } !

Defender Method Default Method

(Virtual) Extension Method!30

Page 31: Java 8 - Return of the Java

Java 8 extension methodsExtend interfaces with new methods

Compatibility

Default implementation

Override

Requires modification of original interface!31

Page 32: Java 8 - Return of the Java

C# extension methods“Add” methods to existing types

Without modifying original type

Defined as static methods

Called as instance methods

!32

Page 33: Java 8 - Return of the Java

Scala implicit classes“Add” methods or properties to existing types

Without modifying original type

Implicit wrapper object

!33

Page 34: Java 8 - Return of the Java

Java 7 vs 8Collections.sort(people, new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } });

!vs

!people.sort(comparing(Person::getName));

!34

Page 35: Java 8 - Return of the Java

So far, so good?Lambdas

Method handles

Extension methods

!35

Page 36: Java 8 - Return of the Java

So, what’s the catch?© Fredrik Vraalsen 2012

Page 37: Java 8 - Return of the Java

What’s wrong with using List::sort ?Modifies existing collection

Others may be using it?

Concurrency issues

Performance

!37

Page 38: Java 8 - Return of the Java

Streams

© Fredrik Vraalsen 2008

Page 39: Java 8 - Return of the Java

The old fashioned way

!39

List<RoadData> filtered = new ArrayList<>();int count = 0; for (Iterator<RoadData> i = roadData.iterator(); i.hasNext() && count < 10; ) { RoadData data = i.next(); if (data.getName().contains(nameQuery)) { filtered.add(data); count++; } }

Page 40: Java 8 - Return of the Java

Streams

!40

!roadData.stream() .filter(r -> r.getName().contains(nameQuery)) .limit(10) .collect(Collectors.toList());

Page 41: Java 8 - Return of the Java

Streams – pipelines

!41

!roadData.stream() .filter(r -> r.getName().contains(nameQuery)) .limit(10) .collect(Collectors.toList());!!cat roadData.txt | grep … | head > output.txt

Page 42: Java 8 - Return of the Java

Streams – pipelinesSource

collection, array, generator function, IO channel, ...

Intermediate operations (Stream-producing)

filter, map, ...

Terminal operations (value-producing)

!42

Page 43: Java 8 - Return of the Java

Performance

© Fredrik Vraalsen 2012

Page 44: Java 8 - Return of the Java

Streams – performance!people.stream() .filter(p -> p.getAge() >= 18) .map(Person::getName) .collect(Collectors.toList());

!44

Page 45: Java 8 - Return of the Java

This one goes to 11!!people.parallelStream() .filter(p -> p.getAge() >= 18) .map(Person::getName) .collect(Collectors.toList());

!45

Page 46: Java 8 - Return of the Java

Scala FTW!!people.par .filter(_.age >= 18) .map(_.name)

!46

Page 47: Java 8 - Return of the Java

Streamsjava.util.stream

Create new results

Lazy

Parallelizable

!47

Page 48: Java 8 - Return of the Java

More cool stuff in Java 8String join

Collection removeIf (≈ filter)

List sort

Map getOrDefault, putIfAbsent, replace, forEach, etc.

java.util.stream.Collectors

count, sum, average, min, max, groupingBy, etc.

java.nio.file.Files lines!48

Page 49: Java 8 - Return of the Java

What’s missing?© Fredrik Vraalsen 2012

Page 50: Java 8 - Return of the Java

What’s missing?Immutability

Value types

Data structures (lists, maps, etc.)

Concurrency

!50

Page 51: Java 8 - Return of the Java

Some help to be foundImmutable collections

Google Guava, FunctionalJava, clj-ds

Concurrency mechanisms

Akka (Actors, STM)

!51

Page 52: Java 8 - Return of the Java

github.com/krukow/clj-dsPersistentVector<Person> people = Persistents.vector( new Person("Fredrik", 37), new Person("Hedda", 2));

!52

Page 53: Java 8 - Return of the Java

github.com/krukow/clj-dsPersistentVector<Person> people = Persistents.vector( new Person("Fredrik", 37), new Person("Hedda", 2));!PersistentVector<Person> morePeople = people.plus(new Person("Johannes", 4));

!53

Page 54: Java 8 - Return of the Java

github.com/krukow/clj-dsPersistentVector<Person> people = Persistents.vector( new Person("Fredrik", 37), new Person("Hedda", 2));!PersistentVector<Person> morePeople = people.plus(new Person("Johannes", 4));!morePeople.stream() .forEach(p -> System.out.println(p.getName()));

!54

Page 55: Java 8 - Return of the Java

Ready to make the jump!?

© Fredrik Vraalsen 2013

Page 56: Java 8 - Return of the Java

Play with it!Download – http://www.oracle.com/technetwork/java/

… or https://jdk8.java.net/download.html

Whitepapers – http://openjdk.java.net/projects/lambda/

FAQ – http://www.lambdafaq.org/

Supported in IntelliJ IDEA 12 & 13

Eclipse Java 8 beta plugin and NetBeans 8.0 RC !56

Page 57: Java 8 - Return of the Java

Why use X instead?Java 8 just released

Will be a long time before you can use it in enterprise dev!

Clojure and Scala available NOW! (JDK 1.6)

Important things are still missing

!57

Page 58: Java 8 - Return of the Java

Want to know more?^{Oslo "Socially Functional Programmers" #OsloSFP}

“Haskell på godt og vondt” – tonight 6pm @ Teknologihuset

http://www.meetup.com/Oslo-Socially-Functional/

Functional Programming in Java 8

http://2014.flatmap.no/

!58

Page 59: Java 8 - Return of the Java

Questions?

© Fredrik Vraalsen 2012

[email protected] / @fredriv

Page 60: Java 8 - Return of the Java