java8 lambda

Download Java8 lambda

Post on 10-Jun-2015

4.057 views

Category:

Technology

9 download

Embed Size (px)

TRANSCRIPT

  • 1. Java 8 @kojilin 2014/03/29@TWJUG

2. Release Notes What s New Known Issue Compatibility Guide 3. Java SE 8 JSR 308 Annotations on Java Types JSR 310 Date and Time API JSR 335 Lambda Expressions for the Java Programming Language 4. Java SE 8 JSR 114 JDBC Rowsets JSR 160 JMX Remote API JSR 173 Java Compiler API JSR 199 Streaming API for XML JSR 221 JDBC 4.0 JSR 269 Pluggable Annotation-Processing API 5. JEP 6. Lambda 7. Why Lambda? Multicore Java API Fork-join 8. int highScore = 0; for (Student student : students) { if (student.age != 20) { continue; } if (student.score > highScore) { highScore = student.score; } } 9. students.stream() .filter(new Predicate() { @Override public boolean test(final Student student) { return student.age == 20; } }) .mapToInt(new ToIntFunction() { @Override public int applyAsInt(final Student student) { return student.score; } }) .max(); 10. ! students.stream() .filter( student -> student.age == 20) .mapToInt( student -> student.score ) .max(); 11. Functional Interface Java API interface Runnable, Callable, Comparator ... 12. Anonymous Inner Class List list = new ArrayList(); ... list.stream().sorted( new Comparator(){ @Override public void compare(String o1, String o2) { return o1.compareTo(o2); } }); 13. Lambda Expression List list = new ArrayList(); ... list.stream().sorted((o1, o2) -> { return o1.compareTo(o2); }); 14. list.stream().sorted( new Comparator(){ @Override public void compare(String o1, String o2) { return o1.compareTo(o2); } }); sorted Comparator 15. list.stream().sorted( new Comparator(){ @Override public void compare(String o1, String o2) { return o1.compareTo(o2); } }); Comparator compare 16. list.stream().sorted( new Comparator(){ @Override public void compare(String o1, String o2) { return o1.compareTo(o2); } }); compare 17. list.stream().sorted( (o1, o2) -> { return o1.compareTo(o2); }); 18. Anonymous functions Lambda expression functional interface lambda lambda functional interface parameter type 19. new Comparator(){ @Override public void compare(String o1, String o2) { return o1.compareTo(o2); } } ! (o1, o2) -> { return o1.compareTo(o2); } 20. Lambda Syntax (int x, int y) -> x + y 21. (int x, int y) -> x + y Argument List 22. (int x, int y) -> x + y Arrow Token 23. (int x, int y) -> x + y Body 24. (int x, int y) -> x + y () -> 42 (String s) -> { System.out.println(s); } Body is expression Body is statement block 25. (String s) -> System.out.println(s) Body is a void method invocation 26. ! (o1, o2) -> { return o1.compareTo(o2); } ! ! ! (o1, o2) -> o1.compareTo(o2) 27. ! (o1, o2) -> { return o1.compareTo(o2); } ! ! ! (o1, o2) -> o1.compareTo(o2) lambda _ 28. java.util.function.* Predicate Function Supplier UnaryOperator BinaryOperator Optional etc. 29. Predicate interface Predicate { boolean test(T t); } age -> age >= 18 ! Arrays.asList(12,25,37) .stream() .filter(age -> age >= 18); 30. Function interface Function { R apply(T t); } title -> title.length() ! Arrays.asList("a","b","c") .stream() .map(title -> title.length()); 31. Supplier interface Supplier { T get(); } () -> new Random().nextInt() ! Stream.generate(() -> new Random().nextInt()); 32. UnaryOperator interface UnaryOperator extends Function{ } t -> t * 10 ! Stream.iterate(1, i -> i * 10); 33. BiFunction interface BiFunction{ R apply(T t, U u); } k, v -> v + "a" ! map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg)); 34. BinaryOperator interface BinaryOperator extends BiFunction{ } i, j -> i + j ! Arrays.asList(12,25,37) .stream() .reduce(i, j -> i + j); 35. interface Hoge { void hogehoge(); } 36. ! ! @FunctionalInterface Functional Interface interface Hoge { void hogehoge(); } @FunctionalInterface interface Hoge {} 37. Object public method @FunctionalInterface interface Hoge { boolean equals(Object o); // compile error } 38. lambda expression (o1, o2) -> o1.compareTo(o2) ! item -> item.name ! param -> new Item(param) Method Reference 39. ! list.stream().sorted((o1, o2) -> o1.compareTo(o2)); ! ! ! list.stream().sorted(String::compareTo); 40. ! list.stream().sorted((o1, o2) -> o1.compareTo(o2)); ! ! ! list.stream().sorted(String::compareTo); 41. Static Instance Instance Method References 42. Static method class ItemTool { static int compareByPrice(Item a, Item b) { ... } } Arrays.asList(item1,item2,item3) .sort(ItemTool::compareByPrice); 43. Instance method class ItemTool { int compareByPrice(Item a, Item b) { ... } } ItemTool tool = new ItemTool(); Arrays.asList(item1,item2,item3) .sort(tool::compareByPrice); 44. instance method class Item { int compareTo(Item b) { ... } } Arrays.asList(item1,item2,item3) .sort(Item::compareTo); 45. > R copy(List from, Supplier toFactory){} ! List sourceList = ...; HashSet result = copy(sourceList, HashSet::new); 46. class Item { public Item() {} public Item(String code) {} } ! interface ItemFactory { Item create(String code); } ! ItemFactory factory = Item::new; Item item = factory.create("a"); 47. void someWork(ItemFactory factory){ Item item = factory.create("a"); } someWork(Item::new); 48. ! ! ItemFactory factory = Item::new; ! ! Function factory = Item::new; 49. Comparator comparator = Item::compareByPrice; ! Comparator comparator = itemTool::compareByPrice; ! Comparator comparator = Item::compareTo; ! Supplier factory = HashSet::new; 50. Lambda Anonymous Class 51. Accessing Local Variables List list = new ArrayList(); ... int max = 10; list.stream().filter( (s) -> s.length() < max ); 52. Scope this enclosing class Shadowing local class anonymous class int max = 10; list.stream().filter((s) -> { int max = 12; // compile error return s.length() < this.delta + max; }); 53. //Outer$1@78308db1 Runnable r1 = new Runnable() { @Override public void run() { System.out.println(this); } } ! //Outer@776ec8df Runnable r2 = () -> System.out.println(this); 54. nal & eectively nal int max = 10; final int min = 2; list.stream().filter((s) -> { max = 12; // compile error return s.length() < max && s.length() > min; }); max = 15; // compile error 55. Illegal forward reference class Foo { Runnable job = () -> { // compile error!! System.out.println(x); }); int x = 10; } 56. Illegal forward reference class Foo { Runnable job = new Runnable { public void run(){ System.out.println(x); } }; int x = 10; } 57. (s) -> System.out.println(s) 1. void forEach(Consumer) 2. void onClick(Listener) Target Typing lambda expression lambda expression 58. @FunctionalInterface interface Foo { void bar(String arg); } // Comsumer Arrays.asList("a","b","c") .stream() .foreach(o -> ); // Foo Foo foo = o -> ; 59. lambda expression Variable declarations Assignments Return statements Array initializers Method or constructor arguments Lambda expression bodies Conditional expressions, ? : Cast expressions 60. void invoke(Runnable runnable); void invoke(Callable callable); // Callable invoke(() -> 3); // Runnable invoke(() -> System.out.println(3)); // ??? invoke(() -> { while(true){ } }); 61. void invoke(Runnable runnable); void invoke(Callable callable); // Callable invoke(() -> 3); // Runnable invoke(() -> System.out.println(3)); // ??? invoke((Runnable)() -> { while(true){ } }); 62. Java 7 List l1 = new ArrayList(); List l2 = new ArrayList(1, 2); calculate(new ArrayList()); List result = a == 10 ? new ArrayList() : Collections.emptyList(); 63. Java 8 List ages = new ArrayList(); List l2 = new ArrayList(1, 2); calculate(new ArrayList()); List result = a == 10 ? new ArrayList() : Collections.emptyList(); 64. Default Methods interface List API 65. Java 8 Default Methods interface public interface Foo{ default int sum(int a, int b){ return a + b; } } 66. class Bar implements Foo{ // int sum(int a, int b){ return a * b; } } 67. lambda expression @FunctionalInterface interface Foo{ int bar(); default int sum(int a, int b){ return a + b; } } Foo foo = () -> { return 3 + sum(1, 2); // compile error!! }; 68. Object interface Foo{ int bar(); // compile error!! default String toString(){ return ; } } 69. interface A{ default String bar(){ return ; } } abstract class B{ String bar(){ return ; } } class C extends B implements A{ } new C().bar(); 70. interface A{ default String bar(){ return ; } } abstract class B{ String bar(){ return ; } } class C extends B implements A{ } new C().bar();// B bar 71. interface A{ default String bar(){ return ; } } interface B{ default String bar(){ return ; } } // Compile error!! class C extends B, A{ } 72. interface A{ default String bar(){ return ; } } interface B{ default String bar(){ return ; } } // class C extends B, A{ default String bar(){...} } 73. Static interface method interface static method interface Foo{ static int sum(int a, int b){ return a + b; } } Foo.sum(1, 2); 74. interface static method interface Bar extends Foo{ } Bar.sum(1, 2); // compile error!! ! class Hoge implements Foo{ } Hoge.sum(1, 2); // compile error!! 75. Lambda API 76. ! students.stream() .filter( student -> student.age == 20) .mapToInt( student -> student.score ) .max(); Stream 77. Optional filter(Bitmap bitmap){} Optional scale(Bitmap bitmap){} ! Optional maybeBitmap = readFromFile(); maybeBitmap.flatMap(bitmap -> filter(bitmap)) .flatMap(bitmap -> scale(bitmap)) .ifPresent(bitmap -> view.setImage(bitmap)); Optional 78. CompletableFuture.supplyAsync(() -> ) .thenApplyAsync(