Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lambda expression par Christophe Huntzinger

2,372 views

Published on

Auteur : Christophe Huntzinger

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Lambda expression par Christophe Huntzinger

  1. 1. Lambda expressionA JDK 8 feature,syntaxic sugar or revolution?@ FinistJUGApril 2013
  2. 2. myselfChristophe HuntzingerSoftware architect and Java evangelistCurrently working @ Thales04/2013Ch. Huntzinger 2
  3. 3. Some historyYou said lambda in java, but● Smalltalk said block or closure● C# said closure● “Grubby” said closureIts a block of code, that can be used a methodparameter, thread safe, and not wrapped in a classSo no more time waiting it in Java!04/2013Ch. Huntzinger 3
  4. 4. Whats that, lambda?“In computer science, a closure (also lexical closure orfunction closure) is a function or reference to afunction together with a referencing environment ofthat function.A closure—unlike a plain function pointer—allows afunction to access those non-local variables evenwhen invoked outside of its immediate lexical scope.”From wikipedia04/2013Ch. Huntzinger 4
  5. 5. How it could deserveExplicit function in code,provide function as method parameters,manage better concurrency04/2013Ch. Huntzinger 5
  6. 6. Some old battleThe three possible shape of lambda● BGGA: Gilad Bracha, Neal Gafter, James Gosling, Peter von der Ahébutton.addActionListener(ActionEvent ev => {// handle the event});● CICE: simplified inner classesbutton.addActionListener(ActionListener(ActionEvent ev) {// handle the event});● FCM: First Class Methodbutton.addActionListener(#(ActionEvent ev) {// handle the event});04/2013Ch. Huntzinger 6
  7. 7. Your first lambdaSimple shape:Argument List Body of closure(int x, int y) -> x+yCh. Huntzinger704/2013Complex shape:Argument List Body of closure: multi lines possible(int x, int y) -> {System.err.println(x+”+”y);};
  8. 8. Lambda by sample● Event04/2013Ch. Huntzinger 8ActionListener al = new ActionListener(){public void actionPerformed(ActionEvent e){System.err.println(“clicked!”);}};ActionListener al = (e) -> {System.err.println(“clicked!”);};
  9. 9. Lambda by sample● Runnable04/2013Ch. Huntzinger 9Runnable r = new Runnable(){public void run(){System.err.println(“Hello world!”);}};Runnable r = () -> {System.err.println(“Hello world!”);};
  10. 10. Lambda under the hood● Interface & default, @FunctionalInterface● Not only an Inner class in bytecode04/2013Ch. Huntzinger 10public class SimpleSample {// Lambda Runnableprotected Runnable r2 = () -> {System.out.println("Hello world two!");}}SimpleSampleSimpleSample lambda$3@FunctionalInterfacepublic interface Function<T, R> {R apply(T t);default <V > Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));
  11. 11. New API for lambda● New Collection API: new method injava.util.Map and java.util.List to useclosure new capabilities● java.util.function: a set of interface for basicclosure instantiation. Create your owns!● java.util.stream: a set of interface to processobject’s Stream.04/2013Ch. Huntzinger 11
  12. 12. advantages• More concise code• Ease concurency• Better API capabilities:o Simplify Pipelineo Simplify filteringo Explicit functional code04/2013Ch. Huntzinger 12
  13. 13. Thank you!04/2013Ch. Huntzinger 13Any question?

×