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 Expressions in Java | Java Lambda Tutorial | Java Certification Training | Edureka

197 views

Published on

**** Java Certification Training: https://www.edureka.co/java-j2ee-soa-training ****
This Edureka tutorial on “Lambda Expressions in Java” will introduce you to a new Java feature called Lambda Expressions. It will also talk about the functional interface in Java. Through this tutorial you will learn the following topics:
Java Lambda Expressions
Functional Interface
Lambda Parameters
Lambda as an Object
Lambda Value Capture
Method References as lambdas

Check out our Java Tutorial blog series: https://goo.gl/osrGrS

Check out our complete Youtube playlist here: https://goo.gl/gMFLx3

Published in: Technology
  • Be the first to comment

Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Training | Edureka

  1. 1. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training 01 02 03 05 06 07 Java Lambda Expressions Functional Interface Lambda Parameters Lambda as an Object Lambda Value Capture Method References as Lambdas Topics For Today’s Discussion
  2. 2. Java Lambda Expressions
  3. 3. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training It provides the implementation of a functional interface & simplifies the software development It provides a clear and concise way to represent a method interface via an expression Java Lambda Expressions It is an anonymous function that doesn’t have a name and doesn’t belong to any class Java lambda expressions are Java's first step into functional programming
  4. 4. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training parameter -> expression body-> Java Lambda Expressions Syntax Characteristics -> Optional Type Declarations -> Optional Parenthesis Around Parameters -> Optional Curly Braces -> Optional return keyword Arrow Operator is introduced in Java through lambda expressions that divides it into two parts i.e Parameters & Body
  5. 5. Functional Interface
  6. 6. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Functional Interface Functional Interface is an interface that contains exactly one abstract method It can have any number of default or static methods along with object class methods Java provides predefined functional interfaces to deal with functional programming Runnable, ActionListener, Comparable are some of the examples of functional interfaces 01 02 03 04
  7. 7. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Functional Interface @FunctionalInterface interface displayable{ void display(String msg); } public class Test implements displayable{ public void display(String msg){ System.out.println(msg); } public static void main(String[] args) { Test dis = new Test(); dis.display("Welcome to Lambda Tutorial by Edureka!"); } }
  8. 8. Lambda parameters
  9. 9. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Lambda Parameters Lambda Expressions can take parameters just like methods 1 2 3 Zero Parameters One Parameter Multiple Parameters
  10. 10. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Lambda Parameters 1 2 3 Zero Parameters One Parameter Multiple Parameters
  11. 11. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Lambda Parameters 1 2 3 Zero Parameters One Parameter Multiple Parameters () -> System.out.println("Zero parameter lambda");
  12. 12. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Lambda Parameters 1 2 3 Zero Parameters One Parameter Multiple Parameters () -> System.out.println("Zero parameter lambda"); (param) -> System.out.println("One parameter: " + param);
  13. 13. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Lambda Parameters 1 2 3 Zero Parameters One Parameter Multiple Parameters () -> System.out.println("Zero parameter lambda"); (param) -> System.out.println("One parameter: " + param); (p1, p2) -> System.out.println("Multiple parameters: " + p1 + ", " + p2);
  14. 14. Lambda As An object
  15. 15. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Lambda as an Object A Java lambda expression is essentially an object that can be assigned to a variable and passed around public interface LambdaComparator { public boolean compare(int a1, int a2); } LambdaComparator myComparator = (a1, a2) -> return a1 > a2; boolean result = myComparator.compare(2, 5); Interface Implementing class
  16. 16. Lambda Variable Capture
  17. 17. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Variable Capture Java lambda expression can access variables that are declared outside the lambda function body under certain circumstances 1 2 3 Local Variable Instance Variables Static Variables
  18. 18. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Lambda Parameters 1 2 3 Local Variable Instance Variables Static Variables
  19. 19. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Lambda Parameters 1 2 3 Local Variable String myStr = "Welcome to Edureka!"; MyLambda dis = (chars) -> { return myStr + ":" + new String(chars); }; Instance Variables Static Variables
  20. 20. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Lambda Parameters 1 2 3 Local Variable Instance Variables Static Variables public class LambdaStaticConsumerDemo { private String str = "Lambda Consumer"; public void attach(LambdaStaticProducerDemo eventProd){ eventProd.listen(e -> { System.out.println(this.str); }); } }
  21. 21. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Lambda Parameters 1 2 3 Local Variable Instance Variables Static Variables public class LambdaStaticConsumerDemo { private static String myStaticVar = "Edureka!"; public void attach(LambdaStaticProducerDemo eventProd){ eventProd.listen(e -> { System.out.println(myStaticVar); }); } }
  22. 22. Method references As lambdas
  23. 23. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Method References Java lambda expression can access variables that are declared outside the lambda function body under certain circumstances Static Method Reference 1 2 3 4 Parameter Method Reference Instance Method Reference Constructor Method Reference
  24. 24. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Method References Static Method Reference 1 2 3 4 Parameter Method Reference Instance Method Reference Constructor Method Reference
  25. 25. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Class Lambda Expression Interface Method References - Static Static Method Reference1 2 3 4 Parameter Method Reference Instance Method Reference Constructor Method Reference public interface Display { public int show(String s1, String s2); } public class Test{ public static int doShow(String s1, String s2){ return s1.lastIndexOf(s2); } } Display disp = Test::doShow;
  26. 26. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Method References - Parameter Static Method Reference 1 2 3 4 Parameter Method Reference Instance Method Reference Constructor Method Reference Interface public interface Display { public int show(String s1, String s2); } Lambda Expression Display disp = String::indexOf;
  27. 27. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Method References - Instance Static Method Reference 1 2 3 4 Parameter Method Reference Instance Method Reference Constructor Method Reference Interface public interface Deserializer { public int deserialize(String v1); } Class Lambda Expression public class StringConverter { public int convertToInt(String v1){ return Integer.valueOf(v1); } } StringConverter strConv = new StringConverter(); Deserializer deserializer = strConv::convertToInt;
  28. 28. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Method References - Constructor Static Method Reference 1 2 3 4 Parameter Method Reference Instance Method Reference Constructor Method Reference Interface public interface Factory { public String create(char[] val); } Lambda Expression Factory fact = String::new;

×