Lambda expressions Overview Yousry Ibrahim April 27, 2016
Agenda • What is the meaning of Lambda? • Functional interface. • Examples with built-in interfaces. • Traversing collections with lambda. • Example with new Interface. 2
What is the meaning of Lambda? 3 –In the computer science lambda means function without a name. –So it opens the door to functional programming. –Lambda let you define a Class with a single method or function with a special easy syntax.
Functional interface - In the quote for an early specs for lambda says “Lambda expressions can only appear in places where they will be assigned to a variable whose type is a functional interface.” http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html - So Functional interfaces are These interfaces which have a single abstract method (before java 8 known as SAM Single Abstract Method ) - Some examples of Java built-in Functional interfaces : Runnable, Callable, Comparator, TimerTask 4
Examples with built-in interfaces 5 Runnable Interface: - Before Java8 we used to make Thread as : - Or even by anonymous class without a name of the class - we could use the inner classes as :
Examples with built-in interfaces Con. 6 Runnable Interface: - Using Lambda Expressions with Java8 : - As you can see the code significantly reduced and also become more readable by using lambda – Now let me explain the syntax and what happened exactly in the previse example - In case implementation more than one line
Examples with built-in interfaces Con II. 7 Comparator Interface: - Before Java8 we used to use Comparator in such way:
Examples with built-in interfaces Con III. 8 Comparator Interface: - Using Lambda expressions in Java8:
Traversing collections with lambda. 9 - Before Lambda we could traverse collections as below:
Traversing collections with lambda Con. 10 - Lets have a look on Array List Class:
Traversing collections with lambda Con II. 11 - So we can write the previse code using Lambda as below
Example with new Interface. 12 - We not only can used the built-in Interfaces But also can use new Interfaces with below steps: 1- Create your new Functional Interface using new annotation 2- Use your interface with Lambda Expressions: The result is 50
Thank you Yousry 13

Lambda expressions java8 - yousry

  • 1.
  • 2.
    Agenda • What isthe meaning of Lambda? • Functional interface. • Examples with built-in interfaces. • Traversing collections with lambda. • Example with new Interface. 2
  • 3.
    What is themeaning of Lambda? 3 –In the computer science lambda means function without a name. –So it opens the door to functional programming. –Lambda let you define a Class with a single method or function with a special easy syntax.
  • 4.
    Functional interface - Inthe quote for an early specs for lambda says “Lambda expressions can only appear in places where they will be assigned to a variable whose type is a functional interface.” http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html - So Functional interfaces are These interfaces which have a single abstract method (before java 8 known as SAM Single Abstract Method ) - Some examples of Java built-in Functional interfaces : Runnable, Callable, Comparator, TimerTask 4
  • 5.
    Examples with built-ininterfaces 5 Runnable Interface: - Before Java8 we used to make Thread as : - Or even by anonymous class without a name of the class - we could use the inner classes as :
  • 6.
    Examples with built-ininterfaces Con. 6 Runnable Interface: - Using Lambda Expressions with Java8 : - As you can see the code significantly reduced and also become more readable by using lambda – Now let me explain the syntax and what happened exactly in the previse example - In case implementation more than one line
  • 7.
    Examples with built-ininterfaces Con II. 7 Comparator Interface: - Before Java8 we used to use Comparator in such way:
  • 8.
    Examples with built-ininterfaces Con III. 8 Comparator Interface: - Using Lambda expressions in Java8:
  • 9.
    Traversing collections withlambda. 9 - Before Lambda we could traverse collections as below:
  • 10.
    Traversing collections withlambda Con. 10 - Lets have a look on Array List Class:
  • 11.
    Traversing collections withlambda Con II. 11 - So we can write the previse code using Lambda as below
  • 12.
    Example with newInterface. 12 - We not only can used the built-in Interfaces But also can use new Interfaces with below steps: 1- Create your new Functional Interface using new annotation 2- Use your interface with Lambda Expressions: The result is 50
  • 13.

Editor's Notes

  • #2 This is a sample Title Slide with Picture ideal for including a dark picture with a brief title and subtitle. A selection of pre-approved title slides are available in the HPE Title Slide Library. The location of the library will be communicated later. To insert a slide with a different picture from the HPE Title Slide Library: Open the file HPE_16x9_Title_Slide_Library.pptx From the Slide thumbnails pane, select the slide with the picture you would like to use in your presentation and click Copy (Ctrl+C) Open a copy of the new HPE 16x9 template (Standard or Events) or your current presentation In the Slide thumbnails pane, click Paste (Ctrl+V) A Paste Options clipboard icon will appear. Click the icon and select Keep Source Formatting. (Ctrl+K)
  • #7 Method Signature ()  Which is empty without arguments because our method in this case (run) does not accept arguments, but in case we implement method that accept arguments we would give here arguments names without types because of course the interface as we said has only one method and they types are already known and lambda target is to eliminate the unnecessary syntax .   The second part is the implementation of the method body