Java 8 - Return of the Java

1,081 views
875 views

Published on

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.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,081
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
47
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Java 8 - Return of the Java

  1. 1. !1 RETURNOF THE AVA J JAVA EIGHT
  2. 2. 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
  3. 3. 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
  4. 4. Your Mission Should you choose to accept it Sort a list of people by their names
  5. 5. Java 7 Collections.sort(people, new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }); !5
  6. 6. We are not amused © Fredrik Vraalsen 2012
  7. 7. 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) !7
  8. 8. 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 !8
  9. 9. Lambda Closure Anonymous function !9
  10. 10. SAM Comparator<Person> byName = new Comparator<Person>() { public int compare(Person x, Person y) { return x.getName().compareTo(y.getName()); } }; !10
  11. 11. 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 !11
  12. 12. 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 !12
  13. 13. Lambda Comparator<Person> byName = ! return x.getName().compareTo(y.getName()); }; BodyReturn type? Parameter list (Person x, Person y) -> { !13
  14. 14. Lambda Comparator<Person> byName = (Person x, Person y) -> Parameter list BodyReturn type? x.getName().compareTo(y.getName()); { }; return !14
  15. 15. Lambda Comparator<Person> byName = Parameter list BodyReturn type? (x, y) -> x.getName().compareTo(y.getName());(Person x, Person y) !15
  16. 16. Lambda Comparator<Person> byName = (x, y) -> x.getName().compareTo(y.getName()); ! Collections.sort(people, byName); !16
  17. 17. Lambda Comparator<Person> byName = (x, y) -> x.getName().compareTo(y.getName()); ! sort(people, byName); !17
  18. 18. Lambda ! ! ! sort(people, (x, y) -> x.getName().compareTo(y.getName())); !18
  19. 19. Comparators import static java.util.Comparator.comparing; … ! sort(people, comparing((Person p) -> p.getName()); !19
  20. 20. Cool! ! ! ! ! Now onwards... © Fredrik Vraalsen 2013
  21. 21. Method handles Reference to methods Can be used in place of lambdas !21
  22. 22. Method handles ! ! ! sort(people, comparing((Person p) -> p.getName())); !22
  23. 23. Method handles ! ! ! sort(people, comparing(Person::getName)); !23
  24. 24. Ok, nice... What else? © Fredrik Vraalsen 2012
  25. 25. Extension methods ! ! ! sort(people, comparing(Person::getName)); !25
  26. 26. Extension methods ! ! ! people.sort(comparing(Person::getName)); !26
  27. 27. Extension methods !27 java.util.List: ! default void sort(Comparator<? super E> c)
  28. 28. Extension methods Defender Method Default Method (Virtual) Extension Method !28 java.util.List: ! default void sort(Comparator<? super E> c)
  29. 29. Extension methods java.util.List: ! default void sort(Comparator<? super E> c) { } ! Defender Method Default Method (Virtual) Extension Method !29
  30. 30. Extension methods java.util.List: ! default void sort(Comparator<? super E> c) { Collections.sort(this, c); } ! Defender Method Default Method (Virtual) Extension Method !30
  31. 31. Java 8 extension methods Extend interfaces with new methods Compatibility Default implementation Override Requires modification of original interface !31
  32. 32. C# extension methods “Add” methods to existing types Without modifying original type Defined as static methods Called as instance methods !32
  33. 33. Scala implicit classes “Add” methods or properties to existing types Without modifying original type Implicit wrapper object !33
  34. 34. Java 7 vs 8 Collections.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
  35. 35. So far, so good? Lambdas Method handles Extension methods !35
  36. 36. So, what’s the catch? © Fredrik Vraalsen 2012
  37. 37. What’s wrong with using List::sort ? Modifies existing collection Others may be using it? Concurrency issues Performance !37
  38. 38. Streams © Fredrik Vraalsen 2008
  39. 39. 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++; } }
  40. 40. Streams !40 ! roadData.stream() .filter(r -> r.getName().contains(nameQuery)) .limit(10) .collect(Collectors.toList());
  41. 41. Streams – pipelines !41 ! roadData.stream() .filter(r -> r.getName().contains(nameQuery)) .limit(10) .collect(Collectors.toList()); ! ! cat roadData.txt | grep … | head > output.txt
  42. 42. Streams – pipelines Source collection, array, generator function, IO channel, ... Intermediate operations (Stream-producing) filter, map, ... Terminal operations (value-producing) !42
  43. 43. Performance © Fredrik Vraalsen 2012
  44. 44. Streams – performance ! people.stream() .filter(p -> p.getAge() >= 18) .map(Person::getName) .collect(Collectors.toList()); !44
  45. 45. This one goes to 11! ! people.parallelStream() .filter(p -> p.getAge() >= 18) .map(Person::getName) .collect(Collectors.toList()); !45
  46. 46. Scala FTW! ! people.par .filter(_.age >= 18) .map(_.name) !46
  47. 47. Streams java.util.stream Create new results Lazy Parallelizable !47
  48. 48. More cool stuff in Java 8 String 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
  49. 49. What’s missing? © Fredrik Vraalsen 2012
  50. 50. What’s missing? Immutability Value types Data structures (lists, maps, etc.) Concurrency !50
  51. 51. Some help to be found Immutable collections Google Guava, FunctionalJava, clj-ds Concurrency mechanisms Akka (Actors, STM) !51
  52. 52. github.com/krukow/clj-ds PersistentVector<Person> people = Persistents.vector( new Person("Fredrik", 37), new Person("Hedda", 2)); !52
  53. 53. github.com/krukow/clj-ds PersistentVector<Person> people = Persistents.vector( new Person("Fredrik", 37), new Person("Hedda", 2)); ! PersistentVector<Person> morePeople = people.plus(new Person("Johannes", 4)); !53
  54. 54. github.com/krukow/clj-ds PersistentVector<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
  55. 55. Ready to make the jump!? © Fredrik Vraalsen 2013
  56. 56. 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
  57. 57. 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
  58. 58. 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
  59. 59. Questions? © Fredrik Vraalsen 2012 vraalsen@iterate.no / @fredriv

×