INTRO TO JAVA 8
SHIJIE ZHANG
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
WHAT WILL JAVA 8 BRING US
CLIMATE IS CHANGING
▸ Java was in dominant positions due to its
simplicity, portability, safety and free to use.
▸ JVM-based dynamic language comes up,
known for their simplicity and portability.
(Groovy, Clojure, Scala)
▸ Big data is on the rise. Programmers need
to deal with large collections.
▸ Multicore processor is becoming more and
more popular. Programmers need to an
easier way to do parallel programming.
▸ Java is kind of verbose.
WHAT WILL JAVA 8 BRING US
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
BEHAVIOR PARAMETERIZATION - LAMBDAS
More comprehensive functional interface
▸ Comparator is an interface. More exactly, a functional interface.
BEHAVIOR PARAMETERIZATION - LAMBDAS
More comprehensive functional interface
▸ Functional interface: an interface has exactly one abstract method
▸ Several functional interface exists before Java 8
▸ Functional interface enables behavior parameterization
BEHAVIOR PARAMETERIZATION - LAMBDAS
BEHAVIOR PARAMETERIZATION - LAMBDAS
Where to use functional interface
▸ Anywhere an object could be used
▸ method arguments/parameters/return types
▸ inside collections
▸ Variables
▸ …….
BEHAVIOR PARAMETERIZATION - LAMBDAS
In the past, use anonymous class as instance for functional interface
Now, use lambda expression/method reference as instance for functional interface
▸ Think about Comparator — create an anonymous class
▸ All anonymous class could be replaced with lambda/method reference
▸ Anonymous class ~ lambda expression ~ method reference
▸ lambda expressions
▸ method reference
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
BEHAVIOR PARAMETERIZATION - LAMBDAS
Lambdas definition
▸ Definition
▸ Examples:
1. ( ) -> { }
2. ( ) -> “Raoul”
3. ( ) -> { return “Mario”;}
4. ( Integer i ) -> return “Alan” + i;
5. ( String s ) -> { “Iron Man”; }
BEHAVIOR PARAMETERIZATION - LAMBDAS
Lambdas definition
▸ Definition
▸ Examples:
1. ( ) -> { }
2. ( ) -> “Raoul”
3. ( ) -> { return “Mario”;}
4. ( Integer i ) -> return “Alan” + i;
5. ( String s ) -> { “Iron Man”; }
BEHAVIOR PARAMETERIZATION - LAMBDAS
Lambda use cases - whenever you use anonymous class
BEHAVIOR PARAMETERIZATION - LAMBDAS
BEHAVIOR PARAMETERIZATION - LAMBDAS
Type reference
Restriction on local variables
▸ Definition
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
BEHAVIOR PARAMETERIZATION - LAMBDAS
Method reference definition
▸ Definition: syntactic sugar for lambda expressions
BEHAVIOR PARAMETERIZATION - LAMBDAS
Rules for converting lambda to method reference
▸ A method reference to a static method
▸ A method reference to an instance method of an arbitrary type
▸ A method reference to an instance method of an existing object
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
SIMPLE SYNTACTIC SUGAR - NEW METHODS INSIDE COLLECTIONS
Map interface: getOrDefault method
▸ Definition: getOrDefault(K key, V defaultValue)
▸ Scenario: get a value (may not exist) from a map, do some calculation and put it back
SIMPLE SYNTACTIC SUGAR - NEW METHODS INSIDE COLLECTIONS
Map interface: computeIfAbsent method
▸ Definition: computeIfAbsent(K key, Function mapping)
▸ Scenario: if the key does not exist, compute a value for it.
SIMPLE SYNTACTIC SUGAR - NEW METHODS INSIDE COLLECTIONS
Map interface: forEach method
▸ Definition: forEach(Consumer con)
▸ Scenario: loop through a map
SIMPLE SYNTACTIC SUGAR - NEW METHODS INSIDE COLLECTIONS
Collections interface: removeIf method
▸ Definition: removeIf(Predicate filter)
▸ Scenario: remove an element from the list if specific condition is met
SIMPLE SYNTACTIC SUGAR - NEW METHODS INSIDE COLLECTIONS
Other method
▸ List.sort(Comparator)
▸ Map.putIfAbsent()
▸ Map.replace() / replaceAll()
▸ Map.merge()
▸ Map.compute() / computeIfAbsent() / computeIfPresent()
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
FUNCTIONAL PROGRAMMING - STREAM API
Stream API
▸ What’s wrong with collections?
▸ Much business logic entails database-like operations such as
grouping a list by category / find the most expensive dish. (Usually
implemented with iterators, could we do it declaratively?)
▸ Big data requires us to utilize multicore processor more frequently.
(Usually implemented with fork/join framework introduced in Java 7.
Could we save some effort? )
FUNCTIONAL PROGRAMMING - STREAM API
Stream API
▸ Def: fancy iterators over collections
▸ Scenario:
FUNCTIONAL PROGRAMMING - STREAM API
FUNCTIONAL PROGRAMMING - STREAM API
FUNCTIONAL PROGRAMMING - STREAM API
How to parallel?
FUNCTIONAL PROGRAMMING - STREAM API
FUNCTIONAL PROGRAMMING - STREAM API
Parallelization realized by Java 7’s fork/join framework underneath
FUNCTIONAL PROGRAMMING - STREAM API
Stream Definition
▸ Stream definition: fancy “internal” iterators over collections
FUNCTIONAL PROGRAMMING - STREAM API
Notice: iterable only once
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Creating, intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
FUNCTIONAL PROGRAMMING - STREAM API
How to create stream?
▸ From arrays
▸ From collections
▸ Custom generators — Stream.generate() / iterate() method
▸ From other popular APIs
FUNCTIONAL PROGRAMMING - STREAM API
Stream operations
▸ intermediate operations
▸ terminal operations
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Creating, intermediate and terminal operations
▸Use cases
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
FUNCTIONAL PROGRAMMING - STREAM API
Use cases: whenever you want to perform database-like operations
▸ Group/Multi-level group
▸ Filter
▸ Sum/Max/Min/Average/Distinct/Count
▸ Extracting specific properties
▸ ……
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Use cases
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
ALTERNATIVE TO NULL — OPTIONAL
Optional definition:
▸ Def: a container may or may not cannot value - just like reference
▸ Benefit 1:
▸ NullPointerException will always be thrown out during runtime
▸ Optional enforces “empty checking” in grammar during compile time
▸ Benefit 2:
▸ Optional interface supports a set of methods makes handling “empty case” easy
ALTERNATIVE TO NULL — OPTIONAL
Optional use case :
▸ http://www.nurkiewicz.com/2013/08/optional-in-java-8-cheat-sheet.html
More use cases:
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
EVOLVING API — DEFAULT METHODS
Default method
EVOLVING API — DEFAULT METHODS
Default method
▸ Definition: A way to evolve Interface APIs in a compatible way.
▸ As a result, interface could now have methods with implementation
▸ This means “Java supports multiple inheritance”
▸ How does Java solves traditional “Diamond Problem”?
▸ Three resolution rules
▸ http://www.javabrahman.com/java-8/java-8-multiple-inheritance-conflict-
resolution-rules-and-diamond-problem/
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
COMPOSABLE ASYNCHRONOUS PROGRAMMING — COMPLETABLEFUTURE
Review: Future
▸ Future is used for asynchronous programming
How to combine multiple Future task???
COMPOSABLE ASYNCHRONOUS PROGRAMMING — COMPLETABLEFUTURE
CompletableFuture comes into play
OTHER NEW FEATURES
Other new features
▸ Date and Time API — Handle time zone, separate concerns
▸ JVM Javascript engine — Nashorn
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
DARKSIDE OF JAVA 8
Dark side of Java 8
▸ Lambda expressions will make debugging log much longer
▸ Not truly functional
▸ Additional reading
▸ http://blog.takipi.com/6-reasons-not-to-switch-to-java-8-just-yet/
▸ Google search “DZone what’s wrong with Java 8”
OUTLINE
▸What will Java 8 give us
▸Behavior parameterization
▸More comprehensive functional interface
▸Lambda expression
▸Method reference
▸Simple syntactic sugar - new methods inside Collections
▸Functional programming - Stream API
▸Intuition
▸Intermediate and terminal operations
▸Properties
▸Alternative to NULL — Optional
▸Changeable Interface — Default methods
▸Asynchronous programming enhancement - Future vs CompletableFuture
▸Other features
▸Dark side of Java 8
▸Conclusion
CONCLUSION
▸ Anonymous class - lambdas
▸ Database-like routines - Stream API
▸ Get to know functional programming
▸ Null pointer exception - Optional
▸ Lots of syntactic sugar in collections method
THANK YOU
Shijie Zhang

Java8

  • 1.
    INTRO TO JAVA8 SHIJIE ZHANG
  • 2.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 3.
    WHAT WILL JAVA8 BRING US CLIMATE IS CHANGING ▸ Java was in dominant positions due to its simplicity, portability, safety and free to use. ▸ JVM-based dynamic language comes up, known for their simplicity and portability. (Groovy, Clojure, Scala) ▸ Big data is on the rise. Programmers need to deal with large collections. ▸ Multicore processor is becoming more and more popular. Programmers need to an easier way to do parallel programming. ▸ Java is kind of verbose.
  • 4.
    WHAT WILL JAVA8 BRING US
  • 5.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 6.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 7.
    BEHAVIOR PARAMETERIZATION -LAMBDAS More comprehensive functional interface ▸ Comparator is an interface. More exactly, a functional interface.
  • 8.
    BEHAVIOR PARAMETERIZATION -LAMBDAS More comprehensive functional interface ▸ Functional interface: an interface has exactly one abstract method ▸ Several functional interface exists before Java 8 ▸ Functional interface enables behavior parameterization
  • 9.
  • 10.
    BEHAVIOR PARAMETERIZATION -LAMBDAS Where to use functional interface ▸ Anywhere an object could be used ▸ method arguments/parameters/return types ▸ inside collections ▸ Variables ▸ …….
  • 11.
    BEHAVIOR PARAMETERIZATION -LAMBDAS In the past, use anonymous class as instance for functional interface Now, use lambda expression/method reference as instance for functional interface ▸ Think about Comparator — create an anonymous class ▸ All anonymous class could be replaced with lambda/method reference ▸ Anonymous class ~ lambda expression ~ method reference ▸ lambda expressions ▸ method reference
  • 12.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 13.
    BEHAVIOR PARAMETERIZATION -LAMBDAS Lambdas definition ▸ Definition ▸ Examples: 1. ( ) -> { } 2. ( ) -> “Raoul” 3. ( ) -> { return “Mario”;} 4. ( Integer i ) -> return “Alan” + i; 5. ( String s ) -> { “Iron Man”; }
  • 14.
    BEHAVIOR PARAMETERIZATION -LAMBDAS Lambdas definition ▸ Definition ▸ Examples: 1. ( ) -> { } 2. ( ) -> “Raoul” 3. ( ) -> { return “Mario”;} 4. ( Integer i ) -> return “Alan” + i; 5. ( String s ) -> { “Iron Man”; }
  • 15.
    BEHAVIOR PARAMETERIZATION -LAMBDAS Lambda use cases - whenever you use anonymous class
  • 16.
  • 17.
    BEHAVIOR PARAMETERIZATION -LAMBDAS Type reference Restriction on local variables ▸ Definition
  • 18.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 19.
    BEHAVIOR PARAMETERIZATION -LAMBDAS Method reference definition ▸ Definition: syntactic sugar for lambda expressions
  • 20.
    BEHAVIOR PARAMETERIZATION -LAMBDAS Rules for converting lambda to method reference ▸ A method reference to a static method ▸ A method reference to an instance method of an arbitrary type ▸ A method reference to an instance method of an existing object
  • 21.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 22.
    SIMPLE SYNTACTIC SUGAR- NEW METHODS INSIDE COLLECTIONS Map interface: getOrDefault method ▸ Definition: getOrDefault(K key, V defaultValue) ▸ Scenario: get a value (may not exist) from a map, do some calculation and put it back
  • 23.
    SIMPLE SYNTACTIC SUGAR- NEW METHODS INSIDE COLLECTIONS Map interface: computeIfAbsent method ▸ Definition: computeIfAbsent(K key, Function mapping) ▸ Scenario: if the key does not exist, compute a value for it.
  • 24.
    SIMPLE SYNTACTIC SUGAR- NEW METHODS INSIDE COLLECTIONS Map interface: forEach method ▸ Definition: forEach(Consumer con) ▸ Scenario: loop through a map
  • 25.
    SIMPLE SYNTACTIC SUGAR- NEW METHODS INSIDE COLLECTIONS Collections interface: removeIf method ▸ Definition: removeIf(Predicate filter) ▸ Scenario: remove an element from the list if specific condition is met
  • 26.
    SIMPLE SYNTACTIC SUGAR- NEW METHODS INSIDE COLLECTIONS Other method ▸ List.sort(Comparator) ▸ Map.putIfAbsent() ▸ Map.replace() / replaceAll() ▸ Map.merge() ▸ Map.compute() / computeIfAbsent() / computeIfPresent()
  • 27.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 28.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 29.
    FUNCTIONAL PROGRAMMING -STREAM API Stream API ▸ What’s wrong with collections? ▸ Much business logic entails database-like operations such as grouping a list by category / find the most expensive dish. (Usually implemented with iterators, could we do it declaratively?) ▸ Big data requires us to utilize multicore processor more frequently. (Usually implemented with fork/join framework introduced in Java 7. Could we save some effort? )
  • 30.
    FUNCTIONAL PROGRAMMING -STREAM API Stream API ▸ Def: fancy iterators over collections ▸ Scenario:
  • 31.
  • 32.
  • 33.
    FUNCTIONAL PROGRAMMING -STREAM API How to parallel?
  • 34.
  • 35.
    FUNCTIONAL PROGRAMMING -STREAM API Parallelization realized by Java 7’s fork/join framework underneath
  • 36.
    FUNCTIONAL PROGRAMMING -STREAM API Stream Definition ▸ Stream definition: fancy “internal” iterators over collections
  • 37.
    FUNCTIONAL PROGRAMMING -STREAM API Notice: iterable only once
  • 38.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Creating, intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 39.
    FUNCTIONAL PROGRAMMING -STREAM API How to create stream? ▸ From arrays ▸ From collections ▸ Custom generators — Stream.generate() / iterate() method ▸ From other popular APIs
  • 40.
    FUNCTIONAL PROGRAMMING -STREAM API Stream operations ▸ intermediate operations ▸ terminal operations
  • 41.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Creating, intermediate and terminal operations ▸Use cases ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 42.
    FUNCTIONAL PROGRAMMING -STREAM API Use cases: whenever you want to perform database-like operations ▸ Group/Multi-level group ▸ Filter ▸ Sum/Max/Min/Average/Distinct/Count ▸ Extracting specific properties ▸ ……
  • 43.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Use cases ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 44.
    ALTERNATIVE TO NULL— OPTIONAL Optional definition: ▸ Def: a container may or may not cannot value - just like reference ▸ Benefit 1: ▸ NullPointerException will always be thrown out during runtime ▸ Optional enforces “empty checking” in grammar during compile time ▸ Benefit 2: ▸ Optional interface supports a set of methods makes handling “empty case” easy
  • 45.
    ALTERNATIVE TO NULL— OPTIONAL Optional use case : ▸ http://www.nurkiewicz.com/2013/08/optional-in-java-8-cheat-sheet.html More use cases:
  • 46.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 47.
    EVOLVING API —DEFAULT METHODS Default method
  • 48.
    EVOLVING API —DEFAULT METHODS Default method ▸ Definition: A way to evolve Interface APIs in a compatible way. ▸ As a result, interface could now have methods with implementation ▸ This means “Java supports multiple inheritance” ▸ How does Java solves traditional “Diamond Problem”? ▸ Three resolution rules ▸ http://www.javabrahman.com/java-8/java-8-multiple-inheritance-conflict- resolution-rules-and-diamond-problem/
  • 49.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 50.
    COMPOSABLE ASYNCHRONOUS PROGRAMMING— COMPLETABLEFUTURE Review: Future ▸ Future is used for asynchronous programming How to combine multiple Future task???
  • 51.
    COMPOSABLE ASYNCHRONOUS PROGRAMMING— COMPLETABLEFUTURE CompletableFuture comes into play
  • 52.
    OTHER NEW FEATURES Othernew features ▸ Date and Time API — Handle time zone, separate concerns ▸ JVM Javascript engine — Nashorn
  • 53.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 54.
    DARKSIDE OF JAVA8 Dark side of Java 8 ▸ Lambda expressions will make debugging log much longer ▸ Not truly functional ▸ Additional reading ▸ http://blog.takipi.com/6-reasons-not-to-switch-to-java-8-just-yet/ ▸ Google search “DZone what’s wrong with Java 8”
  • 55.
    OUTLINE ▸What will Java8 give us ▸Behavior parameterization ▸More comprehensive functional interface ▸Lambda expression ▸Method reference ▸Simple syntactic sugar - new methods inside Collections ▸Functional programming - Stream API ▸Intuition ▸Intermediate and terminal operations ▸Properties ▸Alternative to NULL — Optional ▸Changeable Interface — Default methods ▸Asynchronous programming enhancement - Future vs CompletableFuture ▸Other features ▸Dark side of Java 8 ▸Conclusion
  • 56.
    CONCLUSION ▸ Anonymous class- lambdas ▸ Database-like routines - Stream API ▸ Get to know functional programming ▸ Null pointer exception - Optional ▸ Lots of syntactic sugar in collections method
  • 57.