SlideShare a Scribd company logo
6
Most read
9
Most read
17
Most read
Lambda Expressions – Java 8
Content
• Understanding Lambdas
• Using Lambdas
• Functional Interface
• Method references
• Collections improvements
Why Lambdas?
• Enables Functional Programming
• Readable and concise code
• Enables us to write better APIs and libraries which are easier to use
• Support for parallel processing
Functional Programming VS OOP
• Functional programming allow you to write more readable and
maintainable code.(in some cases)
• Problems in OOP
• Everything is object - all code blocks are "associated" with class and objects ,
which is a problem some time
• We cannot have a independent functions , it have to be a part of function.
• Also allow us to use functions as values.
Function as a value
aBlockOfCode = public void () {
System.out.println("Hello World");
}
perfom
->
aBlockOfCode = () -> {
System.out.println("Hello World");
}
aBlockOfCode = () -> System.out.println("Hello World");
Type Inference
• Your lambda Expression infer type of function and its return type
from the interface(functional interface).
public static void main(String[] args)
{
//StringLengthLambda myStringLenthLambda = (String s) -> s.length();
//StringLengthLambda myStringLenthLambda = (s) -> s.length();
StringLengthLambda myStringLenthLambda = s -> s.length();
System.out.println("String Length :: " + myStringLenthLambda.getLength("Hello World!!"));
}
interface StringLengthLambda
{
int getLength(String s);
}
Lambda Vs Interface Implementation
• We can implement the functional interface in two ways.
• By Implementing the lambda Expression
• By Creating the Anonymous class implementation
Greeting innerClassGreeting = new Greeting() {
@Override
public void perfom() {
System.out.println("Hello World");
}
};
innerClassGreeting.perfom();
Greeting myLambdaFunction = () -> System.out.println("Hello World");
myLambdaFunction.perfom();
Lambda Vs Interface Implementation
• For the most purposes you can think of the lambda expression as the
shortcut for creating these kind of anonymous inner classes but its
not exactly true.
• There are things that inner class does which is different from what
these lambda expressions do.
• In case of lambda expression its not exactly creating an anonymous
class, will look at those differences in a further slides.
Runnable Using Lambdas
• You can use lambda in order to create Runnable.
• Implement Using anonymous inner class
• Implement Using Lambda Expression
public static void main(String[] args) {
Thread myThread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Printed inside runnable");
}
});
myThread.start();
}
Thread myLambdaThread = new Thread(() -> System.out.println("Printed inside Lambda Runnable"));
myLambdaThread.start();
Functional Interface
• Any interface with a SAM(Single Abstract Method) is a functional
interface
• Interface can have multiple default methods but it should have only
one abstract method(Java 8)
• its implementation may be treated as lambda expressions.
• It's recommended that all functional interfaces have an informative
@FunctionalInterface annotation.
• Add @FunctionalInterface on Greeting
Implementation Of Lambda Expression
• Lets do some hands on exercise to understand advantages of lambda
expression with collections.
Predicate Interface
• Represents a predicate (boolean-valued function) of one argument.
• This is a functional interface whose functional method is test(Object).
Consumer Interface
• Represents an operation that accepts a single input argument and
returns no result.
• This is a functional interface whose functional method is
accept(Object).
Exception Handling in lambda Expression
• Create a lambda expression which X in an array of integers and a key
which is and do the mathematical operation for each element in array
given a key.
Closers in Lambda Expressions
• Understand what is effectively final.
• Understand the freeze value in lambda expression
Difference between lambda and Anonymous
Inner Class.
• In an anonymous inner class when using the this reference , it
overwrite the this reference to the anonymous inner class instance.
• In the case of a lambda that does not happen. It still refers to the
instance that it points to the outside of the lambda(no overriding of
this reference that happens).
• Lets have an example in eclipse.
Streams
• A sequence of elements supporting sequential and parallel aggregate
operations.
• Stream Operations
• Intermediate Operations (Return Stream Object)
• Map
• Filter
• Sorted
• Terminal Operations
• Collect
• forEach
• Reduce
• Stream Operations don’t change the source.
Optional
• The purpose of the class is to provide a type-level solution for
representing optional values instead of null references
• Static methods of Optional class
• empty()
• of()
• isPresent()
Date Time API
• Java 8 introduces a new date-time API under the package
java.time. Following are some of the important classes
introduced in java.time package.
• Instant
• LocalDate
• LocalTime
• LocateDateTime

More Related Content

PPTX
Java 8 features
Maýur Chourasiya
 
PDF
Java 8 features
NexThoughts Technologies
 
PDF
Java 8 Lambda Expressions & Streams
NewCircle Training
 
PPTX
Java 8 lambda
Manav Prasad
 
PDF
Lambdas in Java 8
Tobias Coetzee
 
PDF
Java 8
Sheeban Singaram
 
PDF
Java8
Felipe Mamud
 
PPT
Major Java 8 features
Sanjoy Kumar Roy
 
Java 8 features
Maýur Chourasiya
 
Java 8 features
NexThoughts Technologies
 
Java 8 Lambda Expressions & Streams
NewCircle Training
 
Java 8 lambda
Manav Prasad
 
Lambdas in Java 8
Tobias Coetzee
 
Major Java 8 features
Sanjoy Kumar Roy
 

Similar to Lambda Expressions Java 8 Features usage (20)

PPTX
Java 8 presentation
Van Huong
 
PDF
Java 8-revealed
Hamed Hatami
 
PPTX
Lambda expressions java8 - yousry
yousry ibrahim
 
PDF
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
PPTX
Lambda Expressions in Java 8
icarter09
 
PPTX
Java 8 Bootcamp
Mohamed Ben Hassine
 
PPTX
Java 8 lambdas expressions
Lars Lemos
 
PPTX
Java 8 new features
Aniket Thakur
 
PPTX
New features in jdk8 iti
Ahmed mar3y
 
PPTX
Java 8
AbhimanuHandoo
 
PPTX
What’s new in java 8
Rajmahendra Hegde
 
PDF
Programming with Lambda Expressions in Java
langer4711
 
PDF
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Ganesh Samarthyam
 
PDF
Jf12 lambdas injava8-1
langer4711
 
PPTX
Functional programming with_jdk8-s_ritter
Simon Ritter
 
PDF
Intro to Java 8 Closures (Dainius Mezanskas)
Kaunas Java User Group
 
PDF
Intro to Java 8 Lambdas
Dainius Mezanskas
 
PPTX
Java 8 - An Overview
Indrajit Das
 
PPTX
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Simon Ritter
 
PPTX
What's New in Java 8
javafxpert
 
Java 8 presentation
Van Huong
 
Java 8-revealed
Hamed Hatami
 
Lambda expressions java8 - yousry
yousry ibrahim
 
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
Lambda Expressions in Java 8
icarter09
 
Java 8 Bootcamp
Mohamed Ben Hassine
 
Java 8 lambdas expressions
Lars Lemos
 
Java 8 new features
Aniket Thakur
 
New features in jdk8 iti
Ahmed mar3y
 
What’s new in java 8
Rajmahendra Hegde
 
Programming with Lambda Expressions in Java
langer4711
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Ganesh Samarthyam
 
Jf12 lambdas injava8-1
langer4711
 
Functional programming with_jdk8-s_ritter
Simon Ritter
 
Intro to Java 8 Closures (Dainius Mezanskas)
Kaunas Java User Group
 
Intro to Java 8 Lambdas
Dainius Mezanskas
 
Java 8 - An Overview
Indrajit Das
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Simon Ritter
 
What's New in Java 8
javafxpert
 
Ad

Recently uploaded (20)

PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Landforms and landscapes data surprise preview
jpinnuck
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
Ad

Lambda Expressions Java 8 Features usage

  • 2. Content • Understanding Lambdas • Using Lambdas • Functional Interface • Method references • Collections improvements
  • 3. Why Lambdas? • Enables Functional Programming • Readable and concise code • Enables us to write better APIs and libraries which are easier to use • Support for parallel processing
  • 4. Functional Programming VS OOP • Functional programming allow you to write more readable and maintainable code.(in some cases) • Problems in OOP • Everything is object - all code blocks are "associated" with class and objects , which is a problem some time • We cannot have a independent functions , it have to be a part of function. • Also allow us to use functions as values.
  • 5. Function as a value aBlockOfCode = public void () { System.out.println("Hello World"); } perfom -> aBlockOfCode = () -> { System.out.println("Hello World"); } aBlockOfCode = () -> System.out.println("Hello World");
  • 6. Type Inference • Your lambda Expression infer type of function and its return type from the interface(functional interface). public static void main(String[] args) { //StringLengthLambda myStringLenthLambda = (String s) -> s.length(); //StringLengthLambda myStringLenthLambda = (s) -> s.length(); StringLengthLambda myStringLenthLambda = s -> s.length(); System.out.println("String Length :: " + myStringLenthLambda.getLength("Hello World!!")); } interface StringLengthLambda { int getLength(String s); }
  • 7. Lambda Vs Interface Implementation • We can implement the functional interface in two ways. • By Implementing the lambda Expression • By Creating the Anonymous class implementation Greeting innerClassGreeting = new Greeting() { @Override public void perfom() { System.out.println("Hello World"); } }; innerClassGreeting.perfom(); Greeting myLambdaFunction = () -> System.out.println("Hello World"); myLambdaFunction.perfom();
  • 8. Lambda Vs Interface Implementation • For the most purposes you can think of the lambda expression as the shortcut for creating these kind of anonymous inner classes but its not exactly true. • There are things that inner class does which is different from what these lambda expressions do. • In case of lambda expression its not exactly creating an anonymous class, will look at those differences in a further slides.
  • 9. Runnable Using Lambdas • You can use lambda in order to create Runnable. • Implement Using anonymous inner class • Implement Using Lambda Expression public static void main(String[] args) { Thread myThread = new Thread(new Runnable() { @Override public void run() { System.out.println("Printed inside runnable"); } }); myThread.start(); } Thread myLambdaThread = new Thread(() -> System.out.println("Printed inside Lambda Runnable")); myLambdaThread.start();
  • 10. Functional Interface • Any interface with a SAM(Single Abstract Method) is a functional interface • Interface can have multiple default methods but it should have only one abstract method(Java 8) • its implementation may be treated as lambda expressions. • It's recommended that all functional interfaces have an informative @FunctionalInterface annotation. • Add @FunctionalInterface on Greeting
  • 11. Implementation Of Lambda Expression • Lets do some hands on exercise to understand advantages of lambda expression with collections.
  • 12. Predicate Interface • Represents a predicate (boolean-valued function) of one argument. • This is a functional interface whose functional method is test(Object).
  • 13. Consumer Interface • Represents an operation that accepts a single input argument and returns no result. • This is a functional interface whose functional method is accept(Object).
  • 14. Exception Handling in lambda Expression • Create a lambda expression which X in an array of integers and a key which is and do the mathematical operation for each element in array given a key.
  • 15. Closers in Lambda Expressions • Understand what is effectively final. • Understand the freeze value in lambda expression
  • 16. Difference between lambda and Anonymous Inner Class. • In an anonymous inner class when using the this reference , it overwrite the this reference to the anonymous inner class instance. • In the case of a lambda that does not happen. It still refers to the instance that it points to the outside of the lambda(no overriding of this reference that happens). • Lets have an example in eclipse.
  • 17. Streams • A sequence of elements supporting sequential and parallel aggregate operations. • Stream Operations • Intermediate Operations (Return Stream Object) • Map • Filter • Sorted • Terminal Operations • Collect • forEach • Reduce • Stream Operations don’t change the source.
  • 18. Optional • The purpose of the class is to provide a type-level solution for representing optional values instead of null references • Static methods of Optional class • empty() • of() • isPresent()
  • 19. Date Time API • Java 8 introduces a new date-time API under the package java.time. Following are some of the important classes introduced in java.time package. • Instant • LocalDate • LocalTime • LocateDateTime