Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java 8 Lambda

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Java 8 Lambda

Avatar for François Sarradin

François Sarradin

October 06, 2012

Other Decks in Programming

Transcript

  1. JSR337 Java Roadmap Java 8 aug. 2013 Java 7 jul.

    2011 Java 9 ? • FP / Parallel comp. • Date API improv. • Type annotation • Compact profiles • Nashorn Modularity Performance Productivity
  2. Project Lambda What's in it? Project Lambda JSR335 Collection API

    Bulk Operations Lambda Expression Virtual Extension Method Collection API Parallel Collections Functional API
  3. More Expressiveness More Readability 结果 汉字 = new 物().処理(); vs.

    fact n = product [1..n] Is it all Chinese to you?
  4. (Anonymous) Inner Class A λ Solution Iterables.filter(persons, new Predicate<Person>() {

    @Override public boolean apply(Person person) { return person.getAge() >= 18; } }); // Java + Guava
  5. This is what you really need! Iterables.filter(persons, IS_ADULT); Predicate<Person> IS_ADULT

    = new Predicate<Person>() { @Override public boolean apply(Person person) { return person.getAge() >= 18; } }); (Anonymous) Inner Class A (better?) λ Solution
  6. (Anonymous) Inner Class (Not Really) A λ Solution Average length

    of Java one-liner is 21 line! @DEVOPS_BORAT - 12/09/2011 • "The pain of anonymous inner classes makes us roll our eyes in the back of our heads every day." (a wise Oracle's client, 01/2011)
  7. What about Oracle? • "It’s time to add closures to

    Java" (Mark Reinhold, 12/2009) • "Oracle's position is that Java must evolve -- carefully, of course -- in order to remain competitive." (Brian Goetz - 08/2011)
  8. Here is the λ! (int x, int y) -> x

    + y (x, y) -> x + y (int x, int y) -> { return x + y; } (x, y) -> { return x + y; }
  9. Other λ forms m -> { m.put("France", "Paris"); m.put("UK", "London");

    m.put("USA", "Washington"); } x -> 2 * x () -> 42
  10. Inner Class vs. λ Solution Iterables.filter(persons, new Predicate<Person>() { @Override

    public boolean apply(Person p) { return p.getAge() >= 18; } }); // Java 5-7 + Guava Iterables.filter(persons, // Java 8 + Guava p -> p.getAge() >= 18);
  11. Method Reference String::valueOf // like: v -> String.valueOf(v) Integer::compare //

    like: (i1, i2) -> Integer.compare(i1, i2) Arrays.sort(myInts, Integer::compare)
  12. VEM Motivation Help to extend existing APIs Minimize rewrite of

    existing API implementation eg. Collection API eg. Hibernate Collection Implementation VEM
  13. Parallel Computing As Easy as 1-2-3? myCollection.parallel() .filter(x -> ...)

    .map(x -> ...) .reduce((x, y) -> ...) // parallel myCollection.stream() .filter(x -> ...) .map(x -> ...) .reduce((x, y) -> ...) // sequential
  14. Where to Get Java 8 Mercurial repo http://hg.openjdk.java.net/lambda/lambda Java SE

    8 Early Access (with lambda) http://jdk8.java.net/lambda/
  15. Around Project Lambda OpenJDK OSX Build (7 & 8) http://code.google.com/p/openjdk-osx-build/

    Java 8 vs Scala: a Feature Comparison http://www.infoq.com/articles/java-8-vs-scala H. Gomez U. Peter S. van den Berg
  16. Around Project Lambda In France Curious about Project Lambda ;)

    http://blog.xebia.fr/ [FR] http://kerflyn.wordpress.com/ JDK 8: lambdas in Action [FR] http://www.devoxx. com/display/FR12/JDK+8+demo++lambdas+in +Action Java 8 et les Lambda [FR] http://thecodersbreakfast.net/index.php? post/2012/05/30/Java-8-et-les-Lambda G. Tardif O. Croisier Guess Who?
  17. Project Lambda and the Community • Transparency ◦ Presentations /

    Publications ◦ Source code available • Early testing by community ◦ Pre-version available ◦ AdoptOpenJDK (http://java. net/projects/adoptopenjdk/pages/AdoptOpenJDK) • Quick feedback ◦ ML, Hackdays
  18. Question • Oneliner, Readability, and Debugability • Java vs. Competitors

    • What to do for Java 8 to be adopted in projects? • Is there a future for Java?