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

Java 8 Lambda & Streams

Java 8 Lambda & Streams

This presentation is an introduction of the new features of Lambda expressions and Streams of the JDK 8.

Take a look at those two posts about this presentation. They're like a reference material (available soon):

- Lambda Expressions:
http://www.lucassaldanha.com/java-8-lambda-streams-part1/

- Streams:
http://www.lucassaldanha.com/java-8-lambda-streams-part2/

Lucas Saldanha

July 30, 2015
Tweet

More Decks by Lucas Saldanha

Other Decks in Programming

Transcript

  1. Lambdas & Streams! Java 8! © 2015 Lucas Saldanha All

    Rights Reserved! Source code available at:! https://github.com/lucassaldanha/java8-lamda-and-streams-presentation!
  2. Agenda! •  Lambda expressions! •  Functional Interfaces! •  Default Methods!

    •  Variable Capture! •  Method References! •  Streams! © 2015 Lucas Saldanha All Rights Reserved!
  3. Lambda Calculus! •  Introduced in the 30’s by Alonzo Church!

    •  “The smallest universal programming language in the world”! •  A single transformation rule and function definition scheme! •  Doesn’t care about the machine implementing the model! •  All functions are anonymous © 2015 Lucas Saldanha All Rights Reserved!
  4. Lambda Expression! •  An anonymous function (function without name)! • 

    No implementation details in the specification (JSR-335)! •  Two-part function definition:! •  The right part is the body of the function! •  The method’s signature is defined in the FunctionalInterface! •  Lambda expressions are different of anonymous classes! © 2015 Lucas Saldanha All Rights Reserved!
  5. Functional Interface! •  Any interface with one abstract method (independent

    of the JDK version)! •  Can have zero or more default methods! © 2015 Lucas Saldanha All Rights Reserved!
  6. Default Method! •  A way to add new methods on

    an interface without forcing everyone to implement this method (focus on backward compatibility)! •  Method resolution like normal inheritance (uses the closest one)! •  Conflicts:! •  Class with two interfaces and clashing methods names: must @Override the default method! •  Refers a specific default method with: Interface.super.methodName() •  Default methods have access to this (beware the stack overflow error)! © 2015 Lucas Saldanha All Rights Reserved!
  7. Variable Capture! •  Lambdas can interact with variables outside the

    body of the lambda! •  Local variables used in Lambdas must be final or effectively final •  The same rules of Anonymous classes applies except one:! •  The this inside a lambda function refers to the this of the enclosing object! © 2015 Lucas Saldanha All Rights Reserved!
  8. Method Reference! •  Lambda expressions are a way to define

    anonymous functions but it’s not cool rewriting the function multiple times! •  With method reference an existing function can be passed where a lambda is expected! •  The signature of the referenced method must match the signature of the functional interface! •  Even constructors and instance methods can be referenced! © 2015 Lucas Saldanha All Rights Reserved!
  9. Streams! •  Pattern for processing sequences of elements sequentially or

    in parallel! •  Lazy evaluation! •  Basic flow:! 1.  Create a stream from a source (e.g. a collection)! 2.  Add a filter operation to the stream! 3.  Add a map operation to the stream! 4.  Add a terminal operation that ends the stream processing! © 2015 Lucas Saldanha All Rights Reserved!
  10. Stream Structure! •  Structure:! •  Source: from where the stream

    pulls the objects! •  Intermediate operations: operations to be executed on the elements! •  Terminal: operation that pulls values down the stream! •  Lazy evaluation: the result is calculated only when it is terminal operation! © 2015 Lucas Saldanha All Rights Reserved!
  11. Stream Lifecycle! •  Creation: created from a collection, a file,

    a generator...! •  Configuration: collection of pipeline operations! •  Execution: terminal operation is invoked starting pulling objects through the operations pipeline of the stream! •  Cleanup: streams can only be used once! © 2015 Lucas Saldanha All Rights Reserved!
  12. Intermediate Operations! •  Stateless: do not need to know the

    history of results from previous steps in the pipeline or keep track of how many results it have produced or seen (filter, map, flatMap, peek, ...)! •  Stateful: need to know the history of results produced by previous steps in the pipeline or needs to keep track of how many results it has produced or seen (distinct, limit, skip, sorted, ...)! © 2015 Lucas Saldanha All Rights Reserved!
  13. Terminal Operations! •  Reduction: returns a single result! •  Mutable

    reduction: returns multiple results in a container data structure (Collectors class defines many useful collectors)! •  Search: return a result as soon as a match is found! © 2015 Lucas Saldanha All Rights Reserved!
  14. References! •  Java 8 Lambda Expressions & Streams (https://www.youtube.com/watch?v=8pDm_kH4YKY)! • 

    Lambda: A peek under the hood (https://www.youtube.com/watch?v=C_QbkGU_lqY)! •  Invokedynamic 101 ( http://www.javaworld.com/article/2860079/scripting-jvm-languages/invokedynamic-101.html)! •  Java Tutorials: Annonymous classes ( https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)! •  http://www.journaldev.com/2763/java-8-lambda-expressions-and-functional-interfaces-example- tutorial! •  http://radar.oreilly.com/2014/08/java-8-functional-interfaces.html! •  https://dzone.com/articles/introduction-functional-1! © 2015 Lucas Saldanha All Rights Reserved!