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
assass...
Java 8 – what’s new?
Lambdas, Extension Methods, SAMs, Streams, Method Handles, Oh My!
Nashorn JS engine
JSR-310: Date & t...
Java 8 – what’s new?
Lambdas, Extension Methods, SAMs, Streams, Method Handles, Oh My!
Nashorn JS engine
JSR-310: Date & t...
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. se...
Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().com...
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().com...
Java 7
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName()....
Java 7
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName()....
sort(people, byName)
Java 7
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) ...
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()....
Java 8
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName()....
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...
Java 8
java.util.List:
default void sort(Comparator<? super E> c) {
}
Defender Method
Default Method
(Virtual) Extension M...
Java 8
java.util.List:
default void sort(Comparator<? super E> c) {
Collections.sort(this, c);
}
Defender Method
Default M...
Extension method
Extend interfaces with new methods
Compatibility
Default implementation
Override
23
torsdag 12. september...
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
C...
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()....
Single Abstract Method
Interface with only one (non-default) method
Abstract class with only one abstract method
Sound fam...
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().com...
SAM
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName().com...
Lambda
Comparator<Person> byName = new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.getName()....
Lambda
Comparator<Person> byName =
return x.getName().compareTo(y.getName());
};
BodyReturn type?
Parameter list
(Person x...
Lambda
Comparator<Person> byName =
(Person x, Person y) ->
Parameter list
BodyReturn type?
x.getName().compareTo(y.getName...
Lambda
Comparator<Person> byName =
Parameter list
BodyReturn type?
(x, y) -> x.getName().compareTo(y.getName());(Person x,...
Lambda – putting it together
Comparator<Person> byName =
(x, y) -> x.getName().compareTo(y.getName());
people.sort(byName)...
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.get...
Lambda vs Java 7
Collections.sort(people, new Comparator<Person>() {
public int compare(Person x, Person y) {
return x.get...
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
torsd...
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparat...
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparat...
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparat...
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
Comparat...
Method handles
class Person { ...
public int compareByName(Person other) {
return name.compareTo(other.name);
}
}
people.s...
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
5...
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) {
na...
Streams
Stream<Person> stream = people.stream();
Stream<Person> adults = stream.filter(p -> p.getAge() >= 18);
Stream<Stri...
Streams – compose
Stream<String> namesOfAdults = people.stream().
filter(p -> p.getAge() >= 18).
map(Person::getName);
54
...
Streams – collect
List<String> namesOfAdults = people.stream().
filter(p -> p.getAge() >= 18).
map(Person::getName).
colle...
Streams – collect
List<String> namesOfAdults = people.stream().
filter(p -> p.getAge() >= 18).
map(Person::getName).
colle...
Performance
© Fredrik Vraalsen 2012
torsdag 12. september 13
Streams – performance
List<String> namesOfAdults = people.stream().
filter(p -> p.getAge() >= 18).
map(Person::getName).
c...
Streams – performance
List<String> namesOfAdults = people.parallelStream().
filter(p -> p.getAge() >= 18).
map(Person::get...
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
tor...
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-producin...
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...
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!...
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
Conc...
Some help to be found
Immutable collections
Google Guava, FunctionalJava
Concurrency mechanisms
Akka (Actors, STM)
74
tors...
What else is missing?
Type inference
Pattern matching
For comprehensions
Named and default parameters
Properties
Implicit ...
LambdaJ
Easier manipulation of collections and more, limited closure support
Google Guava
Lots of nice utility classes, so...
Questions?
© Fredrik Vraalsen 2012
vraalsen@iterate.no / @fredriv
torsdag 12. september 13
torsdag 12. september 13
Upcoming SlideShare
Loading in …5
×

Java 8 to the rescue!?

903 views

Published on

Java 8 will finally deliver us to the promised land of functional programming! So can we stop worrying now about learning Scala and Clojure and the other new kids on the block?

We'll look at what new powers the new features such as Lambdas, Default Methods and Streams give us, as well as what Java is still lacking compared to other functional languages on the JVM.

Published in: Technology, News & Politics
  • Be the first to comment

  • Be the first to like this

Java 8 to the rescue!?

  1. 1. Java 8 to the rescue!? JavaZone 2013 1 torsdag 12. september 13
  2. 2. 2 RETURNOF THE AVA J JAVA EIGHT torsdag 12. september 13
  3. 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. 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. 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. 6. Your Mission Should you choose to accept it Sort a list of people by their names torsdag 12. september 13
  7. 7. Java 7 List<Person> people = ... Collections.sort(people); 7 torsdag 12. september 13
  8. 8. Java 7 Collections.sort(people, new Comparator<Person>() { }); 8 torsdag 12. september 13
  9. 9. Java 7 Collections.sort(people, new Comparator<Person>() { public int compare(Person x, Person y) { } }); 9 torsdag 12. september 13
  10. 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. 11. We are not amused © Fredrik Vraalsen 2012 torsdag 12. september 13
  12. 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. 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. 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. 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. 16. What about Java 8? © Fredrik Vraalsen 2012 torsdag 12. september 13
  17. 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. 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. 19. Java 8 19 java.util.List: default void sort(Comparator<? super E> c) torsdag 12. september 13
  20. 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. 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. 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. 23. Extension method Extend interfaces with new methods Compatibility Default implementation Override 23 torsdag 12. september 13
  24. 24. C# extension methods Static methods Cannot be overridden One-size fits all solution 24 torsdag 12. september 13
  25. 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. 26. Ok, nice... What else? © Fredrik Vraalsen 2012 torsdag 12. september 13
  27. 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. 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
  29. 29. Lambda Closure Anonymous function 29 torsdag 12. september 13
  30. 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. 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. 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. 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. 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. 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. 36. Lambda – putting it together Comparator<Person> byName = (x, y) -> x.getName().compareTo(y.getName()); people.sort(byName); 36 torsdag 12. september 13
  37. 37. Lambda – putting it together people.sort((x, y) -> x.getName().compareTo(y.getName())); 37 torsdag 12. september 13
  38. 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. 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. 40. Cool! Now onwards... © Fredrik Vraalsen 2013 torsdag 12. september 13
  41. 41. Method handles Reference to methods Can be used in place of lambdas 41 torsdag 12. september 13
  42. 42. Method handles class Person { ... public int compareByName(Person other) { return name.compareTo(other.name); } } 42 torsdag 12. september 13
  43. 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. 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. 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. 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. 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. 48. So far, so good? Extension methods Lambdas Method handles 48 torsdag 12. september 13
  49. 49. So, what’s the catch?© Fredrik Vraalsen 2012 torsdag 12. september 13
  50. 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. 51. Streams © Fredrik Vraalsen 2008 torsdag 12. september 13
  52. 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. 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. 54. Streams – compose Stream<String> namesOfAdults = people.stream(). filter(p -> p.getAge() >= 18). map(Person::getName); 54 torsdag 12. september 13
  55. 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. 56. Streams – collect List<String> namesOfAdults = people.stream(). filter(p -> p.getAge() >= 18). map(Person::getName). collect(toList()); 56 torsdag 12. september 13
  57. 57. Performance © Fredrik Vraalsen 2012 torsdag 12. september 13
  58. 58. Streams – performance List<String> namesOfAdults = people.stream(). filter(p -> p.getAge() >= 18). map(Person::getName). collect(toList()); 58 torsdag 12. september 13
  59. 59. Streams – performance List<String> namesOfAdults = people.parallelStream(). filter(p -> p.getAge() >= 18). map(Person::getName). collect(toList()); 59 torsdag 12. september 13
  60. 60. Scala FTW! val namesOfAdults = people. filter(p => p.age >= 18). map(p => p.name) 60 torsdag 12. september 13
  61. 61. Scala FTW! val namesOfAdults = people. filter(_.age >= 18). map(_.name) 61 torsdag 12. september 13
  62. 62. Scala FTW! val namesOfAdults = people.filter(_.age >= 18).map(_.name) 62 torsdag 12. september 13
  63. 63. Scala FTW! val namesOfAdults = people.par.filter(_.age >= 18).map(_.name).toList 63 torsdag 12. september 13
  64. 64. Streams – sorting List<Person> sorted = people.stream(). sort(Person::compareByName). collect(Collectors.toList()); 64 torsdag 12. september 13
  65. 65. Scala FTW 2! val sorted = people.sortBy(_.name) 65 torsdag 12. september 13
  66. 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
  67. 67. Streams java.util.stream Create new results Lazy Parallelizable 67 torsdag 12. september 13
  68. 68. Ready to make the jump!? © Fredrik Vraalsen 2013 torsdag 12. september 13
  69. 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. 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. 71. What’s missing? © Fredrik Vraalsen 2012 torsdag 12. september 13
  72. 72. What’s missing? Immutability Value types Immutable data structures Lists, maps, sets, etc. 72 torsdag 12. september 13
  73. 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. 74. Some help to be found Immutable collections Google Guava, FunctionalJava Concurrency mechanisms Akka (Actors, STM) 74 torsdag 12. september 13
  75. 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. 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. 77. Questions? © Fredrik Vraalsen 2012 vraalsen@iterate.no / @fredriv torsdag 12. september 13
  78. 78. torsdag 12. september 13

×