331 likes | 1.31k Views
**** Java Certification Training: https://www.edureka.co/java-j2ee-soa-training ****<br>This Edureka tutorial on u201cLambda Expressions in Javau201d 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:<br>Java Lambda Expressions<br>Functional Interface<br>Lambda Parameters<br>Lambda as an Object<br>Lambda Value Capture<br>Method References as lambdas<br><br>Check out our Java Tutorial blog series: https://goo.gl/osrGrS<br><br>Check out our complete Youtube playlist here: https://goo.gl/gMFLx3
E N D
Topics For Today’s Discussion Java Lambda Expressions Lambda as an Object 01 05 Functional Interface Lambda Value Capture 02 06 Lambda Parameters Method References as Lambdas 03 07 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Lambda Expressions
Java Lambda Expressions Java lambda expressions are Java's first step into functional programming It is an anonymous function that doesn’t have a name and doesn’t belong to any class It provides a clear and concise way to represent a method interface via an expression It provides the implementation of a functional interface & simplifies the software development JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Lambda Expressions parameter -> expression body -> Syntax Arrow Operator is introduced in Java through lambda expressions that divides it into two parts i.e Parameters & Body Characteristics -> Optional Type Declarations -> Optional Parenthesis Around Parameters -> Optional Curly Braces -> Optional return keyword JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Functional Interface
Functional Interface 01 02 03 04 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 Functional Interface is an interface that contains exactly one abstract method 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!"); } } JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda parameters
Lambda Parameters Lambda Expressions can take parameters just like methods 1 2 3 Zero Parameters One Parameter Multiple Parameters JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda Parameters 1 Zero Parameters 2 One Parameter 3 Multiple Parameters JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda Parameters Zero Parameters 1 () -> System.out.println("Zero parameter lambda"); 2 One Parameter 3 Multiple Parameters JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda Parameters 1 Zero Parameters () -> System.out.println("Zero parameter lambda"); 2 One Parameter (param) -> System.out.println("One parameter: " + param); 3 Multiple Parameters JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda Parameters 1 Zero Parameters () -> System.out.println("Zero parameter lambda"); 2 One Parameter (param) -> System.out.println("One parameter: " + param); 3 Multiple Parameters (p1, p2) -> System.out.println("Multiple parameters: " + p1 + ", " + p2); JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda As An object
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); } Interface LambdaComparator myComparator = (a1, a2) -> return a1 > a2; Implementing class boolean result = myComparator.compare(2, 5); JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda Variable Capture
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 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda Parameters 1 Local Variable 2 Instance Variables 3 Static Variables JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda Parameters Local Variable 1 String myStr = "Welcome to Edureka!"; 2 Instance Variables MyLambda dis = (chars) -> { return myStr + ":" + new String(chars); }; 3 Static Variables JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda Parameters 1 Local Variable public class LambdaStaticConsumerDemo { private String str = "Lambda Consumer"; 2 Instance Variables public void attach(LambdaStaticProducerDemo eventProd){ eventProd.listen(e -> { System.out.println(this.str); }); } 3 } Static Variables JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Lambda Parameters 1 Local Variable public class LambdaStaticConsumerDemo { private static String myStaticVar = "Edureka!"; 2 public void attach(LambdaStaticProducerDemo eventProd){ eventProd.listen(e -> { System.out.println(myStaticVar); }); } Instance Variables } 3 Static Variables JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Method references As lambdas
Method References Java lambda expression can access variables that are declared outside the lambda function body under certain circumstances 1 2 3 4 Static Method Reference Parameter Method Reference Instance Method Reference Constructor Method Reference JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Method References 1 Static Method Reference 2 Parameter Method Reference 3 Instance Method Reference 4 Constructor Method Reference JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Method References - Static Interface Static Method Reference 1 public interface Display { public int show(String s1, String s2); } 2 Parameter Method Reference Class public class Test{ public static int doShow(String s1, String s2){ return s1.lastIndexOf(s2); } } 3 Instance Method Reference Lambda Expression 4 Constructor Method Reference Display disp = Test::doShow; JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Method References - Parameter 1 Static Method Reference Interface public interface Display { public int show(String s1, String s2); } Parameter Method Reference 2 3 Instance Method Reference Lambda Expression Display disp = String::indexOf; 4 Constructor Method Reference JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Method References - Instance Interface 1 Static Method Reference public interface Deserializer { public int deserialize(String v1); } 2 Parameter Method Reference Class public class StringConverter { public int convertToInt(String v1){ return Integer.valueOf(v1); } } Instance Method Reference 3 Lambda Expression 4 StringConverter strConv = new StringConverter(); Constructor Method Reference Deserializer deserializer = strConv::convertToInt; JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Method References - Constructor 1 Static Method Reference Interface public interface Factory { public String create(char[] val); } 2 Parameter Method Reference Lambda Expression 3 Instance Method Reference Factory fact = String::new; Constructor Method Reference 4 JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training