Java 8 - Lambdas and much more

923 views

Published on

Java 8 - Lambdas, default methods, streams API, method references

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

No Downloads
Views
Total views
923
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
49
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Java 8 - Lambdas and much more

  1. 1. Java 8 Lambdas and much more Alin Pandichi @alinpandichi
  2. 2. About me (Not only Java) software developer Professional experience of 5 years BJUG and AgileWorks community member Twitter: @alinpandichi
  3. 3. We’ll talk about Lambdas Default methods Streams Method references
  4. 4. Java 8 Expected on 18 March 2014 Lambdas: one of the most anticipated features Remember 'Plan B for Java 7'?
  5. 5. Play with it https://github.com/AdoptOpenJDK/lambda-tutorial https://jdk8.java.net/lambda/ IDE support: http://www.jetbrains.com/idea/download/ http://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_(BETA) http://bits.netbeans.org/dev/nightly/latest/
  6. 6. Lambda demo...
  7. 7. External iteration vs. Internal iteration List<Shape> shapes = /* ... */; for (Shape shape :shapes) { shape.setColor(newColor); } shapes.forEach(shape -> shape.setColor(newColor));
  8. 8. Lambda () -> {}
  9. 9. Lambda () -> {} (s) -> { System.out.println(s) }
  10. 10. Lambda () -> {} (s) -> { System.out.println(s) } s -> System.out.println(s)
  11. 11. Lambda (x, y) -> { return x + y; }
  12. 12. Lambda (x, y) -> { return x + y; } (x, y) -> x + y
  13. 13. Lambda (x, y) -> { return x + y; }
  14. 14. Lambda Adder adder = (x, y) -> { return x + y; };
  15. 15. Lambda Adder adder = (x, y) -> { return x + y; }; public interface Adder { public int adder(int x, int y); }
  16. 16. Lambda Adder adder = (x, y) -> { return x + y; }; @FunctionalInterface public interface Adder { public int adder(int x, int y); }
  17. 17. Lambda Adder adder = (x, y) -> { return x + y; }; Adder adder = new Adder() { @Override public int add(int x, int y) { return x + y; } };
  18. 18. Iterable.forEach public interface Iterable<T> { Iterator<T> iterator(); }
  19. 19. Iterable.forEach public interface Iterable<T> { Iterator<T> iterator(); void forEach(Consumer<? super T> action); }
  20. 20. Iterable.forEach public interface Iterable<T> { Iterator<T> iterator(); void forEach(Consumer<? super T> action); } Could break 99% of existing codebases!
  21. 21. Iterable.forEach public interface Iterable<T> { Iterator<T> iterator(); default void forEach(Consumer<? super T> action) { } }
  22. 22. Iterable.forEach public interface Iterable<T> { Iterator<T> iterator(); default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } }
  23. 23. Before Java 8... Interfaces contain only method declarations and no implementations
  24. 24. With Java 8... Interfaces contain only method declarations and no implementations
  25. 25. Default methods demo...
  26. 26. Default methods Also called: Defender methods Virtual extension methods
  27. 27. Default methods Helped extending/improving the existing API interfaces... … without breaking the existing code... … by providing default implementations
  28. 28. Default methods Could be abused, for multiple inheritance!
  29. 29. Default methods Could be abused, for multiple inheritance! Multiple inheritance of type - been there, done that!
  30. 30. Default methods Could be abused, for multiple inheritance! Multiple inheritance of type - been there, done that! of behaviour - you are here!
  31. 31. Default methods Could be abused, for multiple inheritance! Multiple inheritance of type - been there, done that! of behaviour - you are here! of state - not yet, hopefully NOT EVER!
  32. 32. Default methods interface Foo { default void doIt() { /* foo impl */ } } interface Bar { default void doIt() { /* bar impl */ } } class FooBar implements Foo, Bar {} Will not compile
  33. 33. Default methods interface Foo { default void doIt() { /* foo impl */ } } interface Bar { default void doIt() { /* bar impl */ } } class FooBar implements Foo, Bar { @Override public void doIt() { Foo.super.doIt(); } }
  34. 34. Streams API demo...
  35. 35. Streams API Nothing to do with I/O Streams
  36. 36. Streams API Nothing to do with I/O Streams A pipes-and-filters based API for collections
  37. 37. Streams API Nothing to do with I/O Streams A pipes-and-filters based API for collections This may be familiar... ps -ef | grep java | cut -c 1-9 | sort -n | uniq
  38. 38. Streams API Nothing to do with I/O Streams A pipes-and-filters based API for collections This may be familiar... ps -ef | grep java | cut -c 1-9 | sort -n | uniq In a similar manner... blocks.stream() .filter(b -> b.getColor() == Color.RED) .mapToInt(b -> b.getWeight()).sum();
  39. 39. Method references documents .map(d -> d.getTitle()) .collect(toList());
  40. 40. Method references documents .map(d -> d.getTitle()) .collect(toList()); Does nothing but call an existing method...
  41. 41. Method references documents .map(d -> d.getTitle()) .collect(toList()); documents .map(Document::getTitle) .collect(toList());
  42. 42. Method references to a static method ContainingClass::staticMethodName
  43. 43. Method references to a static method ContainingClass::staticMethodName to an instance method of a particular object containingObject::instanceMethodName
  44. 44. Method references to a static method ContainingClass::staticMethodName to an instance method of a particular object containingObject::instanceMethodName to a constructor ClassName::new
  45. 45. See also... ● JSR 310: Date and Time API No more java.util.Calendar!!! ● Nashorn, the new Javascript engine var HelloUser = Java.type("com.credera.example.HelloUser"); var helloUser = new HelloUser("John"); print(helloUser.getMessage()) $ jjs -cp /com/credera/example/HelloUser TestNashorn.js
  46. 46. Recap Lambdas Default methods Streams Method references
  47. 47. Play with it https://github.com/AdoptOpenJDK/lambda-tutorial https://jdk8.java.net/lambda/ IDE support: http://www.jetbrains.com/idea/download/ http://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_(BETA) http://bits.netbeans.org/dev/nightly/latest/

×