Your SlideShare is downloading. ×
Java 8 to the rescue!?
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Java 8 to the rescue!?

606
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? …

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

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
606
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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