java 8 - return of the java
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.TRANSCRIPT
![Page 1: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/1.jpg)
!1
RETURN OF THE AVAJ
JAVA EIGHT
![Page 2: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/4.jpg)
Your Mission Should you choose to accept it
Sort a list of people by their names
![Page 5: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/6.jpg)
We are not amused
© Fredrik Vraalsen 2012
![Page 7: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/9.jpg)
Lambda
Closure
Anonymous function!9
![Page 10: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/16.jpg)
LambdaComparator<Person> byName = (x, y) -> x.getName().compareTo(y.getName()); !Collections.sort(people, byName);
!16
![Page 17: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/17.jpg)
LambdaComparator<Person> byName = (x, y) -> x.getName().compareTo(y.getName()); !sort(people, byName);
!17
![Page 18: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/18.jpg)
Lambda!!!sort(people, (x, y) -> x.getName().compareTo(y.getName()));
!18
![Page 19: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/19.jpg)
Comparatorsimport static java.util.Comparator.comparing; … !sort(people, comparing((Person p) -> p.getName());
!19
![Page 20: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/20.jpg)
Cool! !
!
!
!
Now onwards...© Fredrik Vraalsen 2013
![Page 21: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/21.jpg)
Method handlesReference to methods
Can be used in place of lambdas
!21
![Page 22: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/22.jpg)
Method handles!!!sort(people, comparing((Person p) -> p.getName()));
!22
![Page 23: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/23.jpg)
Method handles!!!sort(people, comparing(Person::getName));
!23
![Page 24: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/24.jpg)
Ok, nice... What else?
© Fredrik Vraalsen 2012
![Page 25: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/25.jpg)
Extension methods!!!sort(people, comparing(Person::getName));
!25
![Page 26: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/26.jpg)
Extension methods!!!people.sort(comparing(Person::getName));
!26
![Page 27: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/27.jpg)
Extension methods
!27
java.util.List: ! default void sort(Comparator<? super E> c)
![Page 28: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/35.jpg)
So far, so good?Lambdas
Method handles
Extension methods
!35
![Page 36: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/36.jpg)
So, what’s the catch?© Fredrik Vraalsen 2012
![Page 37: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/38.jpg)
Streams
© Fredrik Vraalsen 2008
![Page 39: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/40.jpg)
Streams
!40
!roadData.stream() .filter(r -> r.getName().contains(nameQuery)) .limit(10) .collect(Collectors.toList());
![Page 41: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/43.jpg)
Performance
© Fredrik Vraalsen 2012
![Page 44: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/44.jpg)
Streams – performance!people.stream() .filter(p -> p.getAge() >= 18) .map(Person::getName) .collect(Collectors.toList());
!44
![Page 45: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/45.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/46.jpg)
Scala FTW!!people.par .filter(_.age >= 18) .map(_.name)
!46
![Page 47: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/47.jpg)
Streamsjava.util.stream
Create new results
Lazy
Parallelizable
!47
![Page 48: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/48.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/49.jpg)
What’s missing?© Fredrik Vraalsen 2012
![Page 50: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/50.jpg)
What’s missing?Immutability
Value types
Data structures (lists, maps, etc.)
Concurrency
!50
![Page 51: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/51.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/52.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/53.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/54.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/55.jpg)
Ready to make the jump!?
© Fredrik Vraalsen 2013
![Page 56: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/56.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/57.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/58.jpg)
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 60: Java 8 - Return of the Java](https://reader034.vdocuments.mx/reader034/viewer/2022052212/554f5f38b4c905bb178b45e6/html5/thumbnails/60.jpg)