Slide 1

Slide 1 text

Lambdas & Streams! Java 8! © 2015 Lucas Saldanha All Rights Reserved! Source code available at:! https://github.com/lucassaldanha/java8-lamda-and-streams-presentation!

Slide 2

Slide 2 text

Agenda! •  Lambda expressions! •  Functional Interfaces! •  Default Methods! •  Variable Capture! •  Method References! •  Streams! © 2015 Lucas Saldanha All Rights Reserved!

Slide 3

Slide 3 text

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!

Slide 4

Slide 4 text

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!

Slide 5

Slide 5 text

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!

Slide 6

Slide 6 text

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!

Slide 7

Slide 7 text

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!

Slide 8

Slide 8 text

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!

Slide 9

Slide 9 text

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!

Slide 10

Slide 10 text

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!

Slide 11

Slide 11 text

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!

Slide 12

Slide 12 text

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!

Slide 13

Slide 13 text

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!

Slide 14

Slide 14 text

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!