Upload
parth-joshi
View
526
Download
1
Embed Size (px)
Citation preview
Quick Intro to Lambda Expressions in Java 8
Parth Joshi
Techno-Entrepreneur | Trainer
Linkedin | http://www.parthjoshi.in
Anonymous Inner class
• Reprise:
Part of Online video tutorial series: Upgrade to Java 8
Anonymous Inner class• Reprise:
Part of Online video tutorial series: Upgrade to Java 8
Anonymous Inner class• Reprise:
Anonymous Inner Class
Anonymous Inner Class
Part of Online video tutorial series: Upgrade to Java 8
Anonymous Inner class
• Issue:
• If the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear.
• In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button.
Part of Online video tutorial series: Upgrade to Java 8
Anonymous Inner class
• Solution in Java 8:
• Lambda expressions enable you to do this,
1. to treat functionality as method argument,
2. or code as data.
Part of Online video tutorial series: Upgrade to Java 8
Lambda Expression
• Conventional Anonymous class
Part of Online video tutorial series: Upgrade to Java 8
Lambda Expression
• Conventional Anonymous class
Lambda Expression
Part of Online video tutorial series: Upgrade to Java 8
Lambda Expression
• Important notes:
• You need to use this with functional interface only. Anonymous inner classes with abstract class instantiation will not work here.
Part of Online video tutorial series: Upgrade to Java 8
Lambda Expression
• Important notes:
• We cannot use multiple function declarations in the interface. There will be ambiguity in lambda expressions in that case:
Compile Time Error: incompatible types: PrintMessage is not a functional interfacemultiple non-overriding abstract methods found in interface PrintMessage
Part of Online video tutorial series: Upgrade to Java 8
Lambda Expression Characteristics
Optional Type Declaration
Optional parenthesis around parameter
Optional curly braces
Optional return value
Part of Online video tutorial series: Upgrade to Java 8
Lambda Expression Characteristics
Optional Type Declaration
Optional parenthesis around parameter
Optional curly braces
Optional return value
Consider the following declarations:
interface PrintMessage {
public void print(String message);
// public void print2(String message 2)
}
interface SimpleMaths {
public int arithmatic(int a, int b);
}
Part of Online video tutorial series: Upgrade to Java 8
Lambda Expression Characteristics
Optional Type Declaration
Optional parenthesis around parameter
Optional curly braces
Optional return value
Consider the following declarations:
interface PrintMessage {
public void print(String message);
// public void print2(String message 2)
}
interface SimpleMaths {
public int arithmatic(int a, int b);
}
We can pass arguments without type declarations
//with type declaration
SimpleMaths addition = (int a, int b) -> {return a + b;};
//without type declaration
SimpleMaths subtraction = (a, b) -> {return a - b;};
Lambda Expression Characteristics
Optional Type Declaration
Optional parenthesis around parameter
Optional curly braces
Optional return value
Consider the following declarations:
interface PrintMessage {
public void print(String message);
// public void print2(String message 2)
}
interface SimpleMaths {
public int arithmatic(int a, int b);
}
For a single argument in the functional interface, () are not required
// message with parenthesis
PrintMessage helloMessage =
(String message) -> {System.out.println("Hello world");};
// optional paranthesis around parameter.
// this only works with single argumnent
PrintMessage sayHi = message-> {System.out.println("Hi");};
Lambda Expression Characteristics
Optional Type Declaration
Optional parenthesis around parameter
Optional curly braces
Optional return value
Consider the following declarations:
interface PrintMessage {
public void print(String message);
// public void print2(String message 2)
}
interface SimpleMaths {
public int arithmatic(int a, int b);
}
No need to use {} in case of single statement expression
PrintMessage sayHi = message-> System.out.println("Hi");
Lambda Expression Characteristics
Optional Type Declaration
Optional parenthesis around parameter
Optional curly braces
Optional return value
Consider the following declarations:
interface PrintMessage {
public void print(String message);
// public void print2(String message 2)
}
interface SimpleMaths {
public int arithmatic(int a, int b);
}
In case of single expression, compiler returns the value of expression
// single statement, returns the value of expression.
SimpleMaths multiplication = (a , b) -> a*b;
Part of Online video tutorial series: Upgrade to Java 8
Found the content Valuable ?Explore the complete course in Online Lecture series:
Upgrade to Java 8
https://www.udemy.com/upgrade-to-java-8/
Salient Features:• Quickly learn concepts such as
• Lambda Expressions• Functional Interfaces• Method References• Streams
• Around 37 Lectures !• Lifetime access• Ask questions online!• Certification of Completion from Udemy!• Also available on iOS and Android