SlideShare a Scribd company logo
1 of 30
Download to read offline
© Haim Michael 2017. All Rights Reserved.
Java 8 Lambda Expressions
Haim Michael
January 1st
, 2017
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
LifeMichael.com
Video Part 1
https://youtu.be/x-00QOiylRw
Video Part 2
https://youtu.be/7s66dPXkygc
© Haim Michael 2017. All Rights Reserved.
Haim Michael Introduction
● Snowboarding. Learning. Coding. Teaching. More than
16 years of Practical Experience.
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Haim Michael Introduction
● Professional Certifications
Zend Certified Engineer in PHP
Certified Java Professional
Certified Java EE Web Component Developer
OMG Certified UML Professional
● MBA (cum laude) from Tel-Aviv University
Information Systems Management
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Introduction
 When dealing with an interface that includes one
method only and the sole purpose of that interface is to
allow us passing over functionality to another part of
our program we can use the lambda expression as an
alternative for the anonymous inner class.
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Syntax
 The lambda expression includes a comma separated list
of formal parameters enclosed within parentheses (the
data type of the parameters can be usually omitted). If
there is only one parameter then we can omit the
parentheses.
ob -> ob.age<40
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Syntax
 The body includes a single expression or a statement
block. We can include the return statement.
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Simple Demo
package com.lifemichael.samples;
public class SimpleLambdaExpressionDemo {
interface MathOperation {
int execute(int a, int b);
}
public int calculate(int a, int b, MathOperation op) {
return op.execute(a, b);
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Simple Demo
public static void main(String... args) {
SimpleLambdaExpressionDemo myApp =
new SimpleLambdaExpressionDemo();
MathOperation addition = (a, b) -> a + b;
MathOperation subtraction = (a, b) -> a - b;
int num = addition.execute(40, 2);
System.out.println("40 + 2 = " + num);
System.out.println("20 – 10 = " +
myApp.calculate(20, 10, subtraction));
}
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Simple Demo
The Output
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Methods References
 There are four types of methods references: reference to
static method, reference to an instance method of a
particular object, reference to an instance method of an
arbitrary object of a specific type and reference to
constructor.
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Static Method
 We can refer a static method by specifying the name of
the class following with "::" and the name of the static
method.
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Static Method
package com.lifemichael.samples;
public class SimpleLambdaExpressionDemo {
interface MathOperation {
int execute(int a, int b);
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Static Method
public static void main(String... args) {
MathOperation op = Utils::calcu;
int num = op.execute(40, 2);
System.out.println("40 * 2 = " + num);
}
}
class Utils
{
public static int calcu(int a,int b)
{
return a*b;
}
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Static Method
The Output
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Instance Method
 We can refer a specific instance method by specifying the
reference for the object following with "::" and the name of
the instance method.
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Instance Method
package com.lifemichael.samples;
public class SimpleLambdaExpressionDemo {
interface MathOperation {
int execute(int a, int b);
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Instance Method
public static void main(String... args)
{
Utils utils = new Utils();
MathOperation op = utils::calcu;
int num = op.execute(40, 2);
System.out.println("40 * 2 = " + num);
}
}
class Utils
{
public int calcu(int a,int b)
{
System.out.println("calcu is called");
return a*b;
}
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Instance Method
The Output
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Instance Method
of an Arbitrary Object
 We can refer a specific instance method without been
specific about the object on which it should be invoked.
 Instead of specifying the reference for a specific object we
should specify the name of the class followed by :: and the
name of the function.
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Instance Method
of an Arbitrary Object
public class SimpleLambdaExpressionDemo {
public static void main(String... args) {
Student[] students = {
new Student(123123,98,"dave"),
new Student(234233,88,"ron"),
new Student(452343,82,"ram"),
new Student(734344,64,"lara")
};
Arrays.sort(students,Student::compareTo);
for(Student std : students) {
System.out.println(std);
}
}
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Instance Method
of an Arbitrary Object
class Student implements Comparable<Student>
{
private double average;
private int id;
private String name;
Student(int id, double avg, String name)
{
this.average = avg;
this.id = id;
this.name = name;
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Instance Method
of an Arbitrary Object
@Override
public String toString() {
return id+" "+average+" "+name;
}
@Override
public int compareTo(Student o) {
if(this.average>o.average) {
return 1;
}
else if(this.average<o.average) {
return -1;
}
else return 0;
}
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Instance Method
of an Arbitrary Object
The Output
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Constructor
 We can refer the default constructor in a class by writing
the name of the class following with ::new.
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Constructor
public class ReferenceToConstructor
{
public static void main(String... args) {
Student []students = new Student[8];
populate(students, Student::new);
for(int i=0; i<students.length; i++) {
System.out.println(students[i]);
}
}
public static void populate(
Student[] vec,
StudentsGenerator ob) {
for(int i=0; i<vec.length; i++) {
vec[i] = ob.create();
}
}
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Constructor
interface StudentsGenerator {
public Student create();
}
class Student implements Comparable<Student>
{
private static int counter = 1;
private static String[] names ={"david","moshe","anat","jake"};
private double average;
private int id;
private String name;
Student() {
id = counter++;
name = names[(int)(names.length*Math.random())];
average = (int)(100*Math.random());
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
Reference to Constructor
Student(int id, double avg, String name)
{
this.average = avg;
this.id = id;
this.name = name;
}
@Override
public String toString()
{
return id+" "+average+" "+name;
}
LifeMichael.com
© Haim Michael 2017. All Rights Reserved.
@Override
public int compareTo(Student o) {
if(this.average>o.average) {
return 1;
}
else if(this.average<o.average) {
return -1;
}
else return 0;
}
}
Reference to Constructor
© Haim Michael 2017. All Rights Reserved.
The Output
Reference to Constructor
© Haim Michael 2017. All Rights Reserved.
Questions & Answers
● If you enjoyed my lecture please leave me a comment
at http://speakerpedia.com/speakers/life-michael.
Thanks for your time!
Haim.
LifeMichael.com

More Related Content

What's hot

Rahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd YearRahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd Yeardezyneecole
 
Ram Prasad ,BCA 2nd Year
Ram Prasad ,BCA 2nd YearRam Prasad ,BCA 2nd Year
Ram Prasad ,BCA 2nd Yeardezyneecole
 
Gremlin Queries with DataStax Enterprise Graph
Gremlin Queries with DataStax Enterprise GraphGremlin Queries with DataStax Enterprise Graph
Gremlin Queries with DataStax Enterprise GraphStephen Mallette
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive WrappersBharat17485
 
Extending Gremlin with Foundational Steps
Extending Gremlin with Foundational StepsExtending Gremlin with Foundational Steps
Extending Gremlin with Foundational StepsStephen Mallette
 
Being Expressive in Code
Being Expressive in CodeBeing Expressive in Code
Being Expressive in CodeEamonn Boyle
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programsharman kaur
 
Gaurav Singh Chouhan, BCA 2nd Year
Gaurav Singh Chouhan, BCA 2nd YearGaurav Singh Chouhan, BCA 2nd Year
Gaurav Singh Chouhan, BCA 2nd Yeardezyneecole
 
Ronak Kachhawa,BCA 2nd Year
Ronak Kachhawa,BCA 2nd YearRonak Kachhawa,BCA 2nd Year
Ronak Kachhawa,BCA 2nd Yeardezyneecole
 

What's hot (14)

Rahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd YearRahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd Year
 
Ram Prasad ,BCA 2nd Year
Ram Prasad ,BCA 2nd YearRam Prasad ,BCA 2nd Year
Ram Prasad ,BCA 2nd Year
 
Gremlin Queries with DataStax Enterprise Graph
Gremlin Queries with DataStax Enterprise GraphGremlin Queries with DataStax Enterprise Graph
Gremlin Queries with DataStax Enterprise Graph
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive Wrappers
 
Extending Gremlin with Foundational Steps
Extending Gremlin with Foundational StepsExtending Gremlin with Foundational Steps
Extending Gremlin with Foundational Steps
 
Being Expressive in Code
Being Expressive in CodeBeing Expressive in Code
Being Expressive in Code
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 
Gaurav Singh Chouhan, BCA 2nd Year
Gaurav Singh Chouhan, BCA 2nd YearGaurav Singh Chouhan, BCA 2nd Year
Gaurav Singh Chouhan, BCA 2nd Year
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Ronak Kachhawa,BCA 2nd Year
Ronak Kachhawa,BCA 2nd YearRonak Kachhawa,BCA 2nd Year
Ronak Kachhawa,BCA 2nd Year
 
C++ Templates 2
C++ Templates 2C++ Templates 2
C++ Templates 2
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Algorithm and Programming (Array)
Algorithm and Programming (Array)Algorithm and Programming (Array)
Algorithm and Programming (Array)
 

Viewers also liked

LinkedIn: Networking, generating leads and building authority
LinkedIn: Networking, generating leads and building authorityLinkedIn: Networking, generating leads and building authority
LinkedIn: Networking, generating leads and building authorityRoy Harryman
 
JavaScript, Dart, TypeScript & CoffeeScript Comparison
JavaScript, Dart, TypeScript & CoffeeScript ComparisonJavaScript, Dart, TypeScript & CoffeeScript Comparison
JavaScript, Dart, TypeScript & CoffeeScript ComparisonHaim Michael
 
AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...Amazon Web Services Korea
 
Java 8 Support at the JVM Level
Java 8 Support at the JVM LevelJava 8 Support at the JVM Level
Java 8 Support at the JVM LevelNikita Lipsky
 
Java 8 for Tablets, Pis, and Legos
Java 8 for Tablets, Pis, and LegosJava 8 for Tablets, Pis, and Legos
Java 8 for Tablets, Pis, and LegosStephen Chin
 
AWS TEchnical Essentials Workshop
AWS TEchnical Essentials Workshop AWS TEchnical Essentials Workshop
AWS TEchnical Essentials Workshop Muhammad Usman Khan
 
TLS303 How to Deploy Python Applications on AWS Elastic Beanstalk - AWS re:In...
TLS303 How to Deploy Python Applications on AWS Elastic Beanstalk - AWS re:In...TLS303 How to Deploy Python Applications on AWS Elastic Beanstalk - AWS re:In...
TLS303 How to Deploy Python Applications on AWS Elastic Beanstalk - AWS re:In...Amazon Web Services
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIGanesh Samarthyam
 
Training Ensimag OpenStack 2016
Training Ensimag OpenStack 2016Training Ensimag OpenStack 2016
Training Ensimag OpenStack 2016Bruno Cornec
 
Workshop Guide: RESTful Java Web Application with Spring Boot
Workshop Guide: RESTful Java Web Application with Spring BootWorkshop Guide: RESTful Java Web Application with Spring Boot
Workshop Guide: RESTful Java Web Application with Spring BootFabricio Epaminondas
 
Java SE 8 for Java EE developers
Java SE 8 for Java EE developersJava SE 8 for Java EE developers
Java SE 8 for Java EE developersJosé Paumard
 
Building Distributed Systems in Scala
Building Distributed Systems in ScalaBuilding Distributed Systems in Scala
Building Distributed Systems in ScalaAlex Payne
 
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSDeveloping Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSShekhar Gulati
 
AWS re:Invent 2016: IoT Visualizations and Analytics (IOT306)
AWS re:Invent 2016: IoT Visualizations and Analytics (IOT306)AWS re:Invent 2016: IoT Visualizations and Analytics (IOT306)
AWS re:Invent 2016: IoT Visualizations and Analytics (IOT306)Amazon Web Services
 
Running Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar SeriesRunning Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar SeriesAmazon Web Services
 
Java 8 ​and ​Best Practices
 Java 8 ​and ​Best Practices Java 8 ​and ​Best Practices
Java 8 ​and ​Best PracticesWSO2
 
AWS re:Invent 2016: Workshop: Deploy a Deep Learning Framework on Amazon ECS ...
AWS re:Invent 2016: Workshop: Deploy a Deep Learning Framework on Amazon ECS ...AWS re:Invent 2016: Workshop: Deploy a Deep Learning Framework on Amazon ECS ...
AWS re:Invent 2016: Workshop: Deploy a Deep Learning Framework on Amazon ECS ...Amazon Web Services
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in ScalaPatrick Nicolas
 
5 Reasons Why You Should Consider Migrating Web Apps to the Cloud on AWS
5 Reasons Why You Should Consider Migrating Web Apps to the Cloud on AWS5 Reasons Why You Should Consider Migrating Web Apps to the Cloud on AWS
5 Reasons Why You Should Consider Migrating Web Apps to the Cloud on AWSrivetlogic
 

Viewers also liked (20)

LinkedIn: Networking, generating leads and building authority
LinkedIn: Networking, generating leads and building authorityLinkedIn: Networking, generating leads and building authority
LinkedIn: Networking, generating leads and building authority
 
JavaScript, Dart, TypeScript & CoffeeScript Comparison
JavaScript, Dart, TypeScript & CoffeeScript ComparisonJavaScript, Dart, TypeScript & CoffeeScript Comparison
JavaScript, Dart, TypeScript & CoffeeScript Comparison
 
Docker on AWS
Docker on AWSDocker on AWS
Docker on AWS
 
AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...
 
Java 8 Support at the JVM Level
Java 8 Support at the JVM LevelJava 8 Support at the JVM Level
Java 8 Support at the JVM Level
 
Java 8 for Tablets, Pis, and Legos
Java 8 for Tablets, Pis, and LegosJava 8 for Tablets, Pis, and Legos
Java 8 for Tablets, Pis, and Legos
 
AWS TEchnical Essentials Workshop
AWS TEchnical Essentials Workshop AWS TEchnical Essentials Workshop
AWS TEchnical Essentials Workshop
 
TLS303 How to Deploy Python Applications on AWS Elastic Beanstalk - AWS re:In...
TLS303 How to Deploy Python Applications on AWS Elastic Beanstalk - AWS re:In...TLS303 How to Deploy Python Applications on AWS Elastic Beanstalk - AWS re:In...
TLS303 How to Deploy Python Applications on AWS Elastic Beanstalk - AWS re:In...
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
 
Training Ensimag OpenStack 2016
Training Ensimag OpenStack 2016Training Ensimag OpenStack 2016
Training Ensimag OpenStack 2016
 
Workshop Guide: RESTful Java Web Application with Spring Boot
Workshop Guide: RESTful Java Web Application with Spring BootWorkshop Guide: RESTful Java Web Application with Spring Boot
Workshop Guide: RESTful Java Web Application with Spring Boot
 
Java SE 8 for Java EE developers
Java SE 8 for Java EE developersJava SE 8 for Java EE developers
Java SE 8 for Java EE developers
 
Building Distributed Systems in Scala
Building Distributed Systems in ScalaBuilding Distributed Systems in Scala
Building Distributed Systems in Scala
 
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSDeveloping Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJS
 
AWS re:Invent 2016: IoT Visualizations and Analytics (IOT306)
AWS re:Invent 2016: IoT Visualizations and Analytics (IOT306)AWS re:Invent 2016: IoT Visualizations and Analytics (IOT306)
AWS re:Invent 2016: IoT Visualizations and Analytics (IOT306)
 
Running Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar SeriesRunning Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar Series
 
Java 8 ​and ​Best Practices
 Java 8 ​and ​Best Practices Java 8 ​and ​Best Practices
Java 8 ​and ​Best Practices
 
AWS re:Invent 2016: Workshop: Deploy a Deep Learning Framework on Amazon ECS ...
AWS re:Invent 2016: Workshop: Deploy a Deep Learning Framework on Amazon ECS ...AWS re:Invent 2016: Workshop: Deploy a Deep Learning Framework on Amazon ECS ...
AWS re:Invent 2016: Workshop: Deploy a Deep Learning Framework on Amazon ECS ...
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in Scala
 
5 Reasons Why You Should Consider Migrating Web Apps to the Cloud on AWS
5 Reasons Why You Should Consider Migrating Web Apps to the Cloud on AWS5 Reasons Why You Should Consider Migrating Web Apps to the Cloud on AWS
5 Reasons Why You Should Consider Migrating Web Apps to the Cloud on AWS
 

Similar to Java 8 Lambda Expressions

Introduction to Object oriented Design
Introduction to Object oriented DesignIntroduction to Object oriented Design
Introduction to Object oriented DesignAmin Shahnazari
 
.Net december 2017 updates - Tamir Dresher
.Net december 2017 updates - Tamir Dresher.Net december 2017 updates - Tamir Dresher
.Net december 2017 updates - Tamir DresherTamir Dresher
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiMuhammed Thanveer M
 
Gg Code Mash2009 20090106
Gg Code Mash2009 20090106Gg Code Mash2009 20090106
Gg Code Mash2009 20090106Jim Shingler
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonCodemotion
 
C# programming : Chapter One
C# programming : Chapter OneC# programming : Chapter One
C# programming : Chapter OneKhairi Aiman
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonAEM HUB
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling RewriterJustin Edelson
 
Intro to JavaScript - Thinkful LA
Intro to JavaScript - Thinkful LAIntro to JavaScript - Thinkful LA
Intro to JavaScript - Thinkful LAThinkful
 

Similar to Java 8 Lambda Expressions (20)

Intake 37 4
Intake 37 4Intake 37 4
Intake 37 4
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Introduction to Object oriented Design
Introduction to Object oriented DesignIntroduction to Object oriented Design
Introduction to Object oriented Design
 
Clean Code
Clean CodeClean Code
Clean Code
 
Intake 38 5 1
Intake 38 5 1Intake 38 5 1
Intake 38 5 1
 
Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4
 
.Net december 2017 updates - Tamir Dresher
.Net december 2017 updates - Tamir Dresher.Net december 2017 updates - Tamir Dresher
.Net december 2017 updates - Tamir Dresher
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
Gg Code Mash2009 20090106
Gg Code Mash2009 20090106Gg Code Mash2009 20090106
Gg Code Mash2009 20090106
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
 
C# programming : Chapter One
C# programming : Chapter OneC# programming : Chapter One
C# programming : Chapter One
 
06slide.ppt
06slide.ppt06slide.ppt
06slide.ppt
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin Edelson
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Java and j2ee_lab-manual
Java and j2ee_lab-manualJava and j2ee_lab-manual
Java and j2ee_lab-manual
 
Intake 38 5
Intake 38 5Intake 38 5
Intake 38 5
 
05slide
05slide05slide
05slide
 
Intro to JavaScript - Thinkful LA
Intro to JavaScript - Thinkful LAIntro to JavaScript - Thinkful LA
Intro to JavaScript - Thinkful LA
 

More from Haim Michael

Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in JavaHaim Michael
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design PatternsHaim Michael
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL InjectionsHaim Michael
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in JavaHaim Michael
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design PatternsHaim Michael
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in PythonHaim Michael
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in PythonHaim Michael
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScriptHaim Michael
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214Haim Michael
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump StartHaim Michael
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHPHaim Michael
 
What is new in Python 3.9
What is new in Python 3.9What is new in Python 3.9
What is new in Python 3.9Haim Michael
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on SteroidHaim Michael
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib LibraryHaim Michael
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908Haim Michael
 
The num py_library_20200818
The num py_library_20200818The num py_library_20200818
The num py_library_20200818Haim Michael
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728Haim Michael
 
Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Haim Michael
 

More from Haim Michael (20)

Anti Patterns
Anti PatternsAnti Patterns
Anti Patterns
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in Java
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
 
Java Jump Start
Java Jump StartJava Jump Start
Java Jump Start
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump Start
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHP
 
What is new in Python 3.9
What is new in Python 3.9What is new in Python 3.9
What is new in Python 3.9
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on Steroid
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib Library
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908
 
The num py_library_20200818
The num py_library_20200818The num py_library_20200818
The num py_library_20200818
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728
 
Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start)
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 

Recently uploaded (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 

Java 8 Lambda Expressions

  • 1. © Haim Michael 2017. All Rights Reserved. Java 8 Lambda Expressions Haim Michael January 1st , 2017 All logos, trade marks and brand names used in this presentation belong to the respective owners. LifeMichael.com Video Part 1 https://youtu.be/x-00QOiylRw Video Part 2 https://youtu.be/7s66dPXkygc
  • 2. © Haim Michael 2017. All Rights Reserved. Haim Michael Introduction ● Snowboarding. Learning. Coding. Teaching. More than 16 years of Practical Experience. LifeMichael.com
  • 3. © Haim Michael 2017. All Rights Reserved. Haim Michael Introduction ● Professional Certifications Zend Certified Engineer in PHP Certified Java Professional Certified Java EE Web Component Developer OMG Certified UML Professional ● MBA (cum laude) from Tel-Aviv University Information Systems Management LifeMichael.com
  • 4. © Haim Michael 2017. All Rights Reserved. Introduction  When dealing with an interface that includes one method only and the sole purpose of that interface is to allow us passing over functionality to another part of our program we can use the lambda expression as an alternative for the anonymous inner class. LifeMichael.com
  • 5. © Haim Michael 2017. All Rights Reserved. Syntax  The lambda expression includes a comma separated list of formal parameters enclosed within parentheses (the data type of the parameters can be usually omitted). If there is only one parameter then we can omit the parentheses. ob -> ob.age<40 LifeMichael.com
  • 6. © Haim Michael 2017. All Rights Reserved. Syntax  The body includes a single expression or a statement block. We can include the return statement. LifeMichael.com
  • 7. © Haim Michael 2017. All Rights Reserved. Simple Demo package com.lifemichael.samples; public class SimpleLambdaExpressionDemo { interface MathOperation { int execute(int a, int b); } public int calculate(int a, int b, MathOperation op) { return op.execute(a, b); } LifeMichael.com
  • 8. © Haim Michael 2017. All Rights Reserved. Simple Demo public static void main(String... args) { SimpleLambdaExpressionDemo myApp = new SimpleLambdaExpressionDemo(); MathOperation addition = (a, b) -> a + b; MathOperation subtraction = (a, b) -> a - b; int num = addition.execute(40, 2); System.out.println("40 + 2 = " + num); System.out.println("20 – 10 = " + myApp.calculate(20, 10, subtraction)); } } LifeMichael.com
  • 9. © Haim Michael 2017. All Rights Reserved. Simple Demo The Output LifeMichael.com
  • 10. © Haim Michael 2017. All Rights Reserved. Methods References  There are four types of methods references: reference to static method, reference to an instance method of a particular object, reference to an instance method of an arbitrary object of a specific type and reference to constructor. LifeMichael.com
  • 11. © Haim Michael 2017. All Rights Reserved. Reference to Static Method  We can refer a static method by specifying the name of the class following with "::" and the name of the static method. LifeMichael.com
  • 12. © Haim Michael 2017. All Rights Reserved. Reference to Static Method package com.lifemichael.samples; public class SimpleLambdaExpressionDemo { interface MathOperation { int execute(int a, int b); } LifeMichael.com
  • 13. © Haim Michael 2017. All Rights Reserved. Reference to Static Method public static void main(String... args) { MathOperation op = Utils::calcu; int num = op.execute(40, 2); System.out.println("40 * 2 = " + num); } } class Utils { public static int calcu(int a,int b) { return a*b; } } LifeMichael.com
  • 14. © Haim Michael 2017. All Rights Reserved. Reference to Static Method The Output LifeMichael.com
  • 15. © Haim Michael 2017. All Rights Reserved. Reference to Instance Method  We can refer a specific instance method by specifying the reference for the object following with "::" and the name of the instance method. LifeMichael.com
  • 16. © Haim Michael 2017. All Rights Reserved. Reference to Instance Method package com.lifemichael.samples; public class SimpleLambdaExpressionDemo { interface MathOperation { int execute(int a, int b); } LifeMichael.com
  • 17. © Haim Michael 2017. All Rights Reserved. Reference to Instance Method public static void main(String... args) { Utils utils = new Utils(); MathOperation op = utils::calcu; int num = op.execute(40, 2); System.out.println("40 * 2 = " + num); } } class Utils { public int calcu(int a,int b) { System.out.println("calcu is called"); return a*b; } } LifeMichael.com
  • 18. © Haim Michael 2017. All Rights Reserved. Reference to Instance Method The Output LifeMichael.com
  • 19. © Haim Michael 2017. All Rights Reserved. Reference to Instance Method of an Arbitrary Object  We can refer a specific instance method without been specific about the object on which it should be invoked.  Instead of specifying the reference for a specific object we should specify the name of the class followed by :: and the name of the function. LifeMichael.com
  • 20. © Haim Michael 2017. All Rights Reserved. Reference to Instance Method of an Arbitrary Object public class SimpleLambdaExpressionDemo { public static void main(String... args) { Student[] students = { new Student(123123,98,"dave"), new Student(234233,88,"ron"), new Student(452343,82,"ram"), new Student(734344,64,"lara") }; Arrays.sort(students,Student::compareTo); for(Student std : students) { System.out.println(std); } } } LifeMichael.com
  • 21. © Haim Michael 2017. All Rights Reserved. Reference to Instance Method of an Arbitrary Object class Student implements Comparable<Student> { private double average; private int id; private String name; Student(int id, double avg, String name) { this.average = avg; this.id = id; this.name = name; } LifeMichael.com
  • 22. © Haim Michael 2017. All Rights Reserved. Reference to Instance Method of an Arbitrary Object @Override public String toString() { return id+" "+average+" "+name; } @Override public int compareTo(Student o) { if(this.average>o.average) { return 1; } else if(this.average<o.average) { return -1; } else return 0; } } LifeMichael.com
  • 23. © Haim Michael 2017. All Rights Reserved. Reference to Instance Method of an Arbitrary Object The Output LifeMichael.com
  • 24. © Haim Michael 2017. All Rights Reserved. Reference to Constructor  We can refer the default constructor in a class by writing the name of the class following with ::new. LifeMichael.com
  • 25. © Haim Michael 2017. All Rights Reserved. Reference to Constructor public class ReferenceToConstructor { public static void main(String... args) { Student []students = new Student[8]; populate(students, Student::new); for(int i=0; i<students.length; i++) { System.out.println(students[i]); } } public static void populate( Student[] vec, StudentsGenerator ob) { for(int i=0; i<vec.length; i++) { vec[i] = ob.create(); } } } LifeMichael.com
  • 26. © Haim Michael 2017. All Rights Reserved. Reference to Constructor interface StudentsGenerator { public Student create(); } class Student implements Comparable<Student> { private static int counter = 1; private static String[] names ={"david","moshe","anat","jake"}; private double average; private int id; private String name; Student() { id = counter++; name = names[(int)(names.length*Math.random())]; average = (int)(100*Math.random()); } LifeMichael.com
  • 27. © Haim Michael 2017. All Rights Reserved. Reference to Constructor Student(int id, double avg, String name) { this.average = avg; this.id = id; this.name = name; } @Override public String toString() { return id+" "+average+" "+name; } LifeMichael.com
  • 28. © Haim Michael 2017. All Rights Reserved. @Override public int compareTo(Student o) { if(this.average>o.average) { return 1; } else if(this.average<o.average) { return -1; } else return 0; } } Reference to Constructor
  • 29. © Haim Michael 2017. All Rights Reserved. The Output Reference to Constructor
  • 30. © Haim Michael 2017. All Rights Reserved. Questions & Answers ● If you enjoyed my lecture please leave me a comment at http://speakerpedia.com/speakers/life-michael. Thanks for your time! Haim. LifeMichael.com