SlideShare a Scribd company logo
© 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
Ad

More Related Content

What's hot (14)

Rahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd YearRahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd Year
dezyneecole
 
Ram Prasad ,BCA 2nd Year
Ram Prasad ,BCA 2nd YearRam Prasad ,BCA 2nd Year
Ram Prasad ,BCA 2nd Year
dezyneecole
 
Gremlin Queries with DataStax Enterprise Graph
Gremlin Queries with DataStax Enterprise GraphGremlin Queries with DataStax Enterprise Graph
Gremlin Queries with DataStax Enterprise Graph
Stephen Mallette
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive Wrappers
Bharat17485
 
Extending Gremlin with Foundational Steps
Extending Gremlin with Foundational StepsExtending Gremlin with Foundational Steps
Extending Gremlin with Foundational Steps
Stephen Mallette
 
Being Expressive in Code
Being Expressive in CodeBeing Expressive in Code
Being Expressive in Code
Eamonn Boyle
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
harman kaur
 
Gaurav Singh Chouhan, BCA 2nd Year
Gaurav Singh Chouhan, BCA 2nd YearGaurav Singh Chouhan, BCA 2nd Year
Gaurav Singh Chouhan, BCA 2nd Year
dezyneecole
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
Knoldus Inc.
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Kumar Gaurav
 
Ronak Kachhawa,BCA 2nd Year
Ronak Kachhawa,BCA 2nd YearRonak Kachhawa,BCA 2nd Year
Ronak Kachhawa,BCA 2nd Year
dezyneecole
 
C++ Templates 2
C++ Templates 2C++ Templates 2
C++ Templates 2
Ganesh Samarthyam
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Nilesh Dalvi
 
Algorithm and Programming (Array)
Algorithm and Programming (Array)Algorithm and Programming (Array)
Algorithm and Programming (Array)
Adam Mukharil Bachtiar
 
Rahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd YearRahul Saini, BCA 2nd Year
Rahul Saini, BCA 2nd Year
dezyneecole
 
Ram Prasad ,BCA 2nd Year
Ram Prasad ,BCA 2nd YearRam Prasad ,BCA 2nd Year
Ram Prasad ,BCA 2nd Year
dezyneecole
 
Gremlin Queries with DataStax Enterprise Graph
Gremlin Queries with DataStax Enterprise GraphGremlin Queries with DataStax Enterprise Graph
Gremlin Queries with DataStax Enterprise Graph
Stephen Mallette
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive Wrappers
Bharat17485
 
Extending Gremlin with Foundational Steps
Extending Gremlin with Foundational StepsExtending Gremlin with Foundational Steps
Extending Gremlin with Foundational Steps
Stephen Mallette
 
Being Expressive in Code
Being Expressive in CodeBeing Expressive in Code
Being Expressive in Code
Eamonn Boyle
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
harman kaur
 
Gaurav Singh Chouhan, BCA 2nd Year
Gaurav Singh Chouhan, BCA 2nd YearGaurav Singh Chouhan, BCA 2nd Year
Gaurav Singh Chouhan, BCA 2nd Year
dezyneecole
 
Ronak Kachhawa,BCA 2nd Year
Ronak Kachhawa,BCA 2nd YearRonak Kachhawa,BCA 2nd Year
Ronak Kachhawa,BCA 2nd Year
dezyneecole
 

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
Roy Harryman
 
JavaScript, Dart, TypeScript & CoffeeScript Comparison
JavaScript, Dart, TypeScript & CoffeeScript ComparisonJavaScript, Dart, TypeScript & CoffeeScript Comparison
JavaScript, Dart, TypeScript & CoffeeScript Comparison
Haim 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 Level
Nikita 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 Legos
Stephen Chin
 
AWS TEchnical Essentials Workshop
AWS TEchnical Essentials Workshop AWS TEchnical Essentials Workshop
AWS TEchnical Essentials Workshop
Muhammad Usman Khan
 
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
Ganesh Samarthyam
 
Training Ensimag OpenStack 2016
Training Ensimag OpenStack 2016Training Ensimag OpenStack 2016
Training Ensimag OpenStack 2016
Bruno 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 Boot
Fabricio 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 developers
José Paumard
 
Building Distributed Systems in Scala
Building Distributed Systems in ScalaBuilding Distributed Systems in Scala
Building Distributed Systems in Scala
Alex 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 AngularJS
Shekhar Gulati
 
Java 8 ​and ​Best Practices
 Java 8 ​and ​Best Practices Java 8 ​and ​Best Practices
Java 8 ​and ​Best Practices
WSO2
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in Scala
Patrick 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 AWS
rivetlogic
 
Introducing Apache Kafka's Streams API - Kafka meetup Munich, Jan 25 2017
Introducing Apache Kafka's Streams API - Kafka meetup Munich, Jan 25 2017Introducing Apache Kafka's Streams API - Kafka meetup Munich, Jan 25 2017
Introducing Apache Kafka's Streams API - Kafka meetup Munich, Jan 25 2017
Michael Noll
 
An Introduction to Deep Learning
An Introduction to Deep LearningAn Introduction to Deep Learning
An Introduction to Deep Learning
Poo Kuan Hoong
 
Tensorflow in production with AWS Lambda
Tensorflow in production with AWS LambdaTensorflow in production with AWS Lambda
Tensorflow in production with AWS Lambda
Fabian Dubois
 
Kubernetes on aws
Kubernetes on awsKubernetes on aws
Kubernetes on aws
Yousun Jeong
 
ABCs of AWS: S3
ABCs of AWS: S3ABCs of AWS: S3
ABCs of AWS: S3
Mark Cohen
 
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
Roy Harryman
 
JavaScript, Dart, TypeScript & CoffeeScript Comparison
JavaScript, Dart, TypeScript & CoffeeScript ComparisonJavaScript, Dart, TypeScript & CoffeeScript Comparison
JavaScript, Dart, TypeScript & CoffeeScript Comparison
Haim 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 Level
Nikita 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 Legos
Stephen Chin
 
AWS TEchnical Essentials Workshop
AWS TEchnical Essentials Workshop AWS TEchnical Essentials Workshop
AWS TEchnical Essentials Workshop
Muhammad Usman Khan
 
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
Ganesh Samarthyam
 
Training Ensimag OpenStack 2016
Training Ensimag OpenStack 2016Training Ensimag OpenStack 2016
Training Ensimag OpenStack 2016
Bruno 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 Boot
Fabricio 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 developers
José Paumard
 
Building Distributed Systems in Scala
Building Distributed Systems in ScalaBuilding Distributed Systems in Scala
Building Distributed Systems in Scala
Alex 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 AngularJS
Shekhar Gulati
 
Java 8 ​and ​Best Practices
 Java 8 ​and ​Best Practices Java 8 ​and ​Best Practices
Java 8 ​and ​Best Practices
WSO2
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in Scala
Patrick 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 AWS
rivetlogic
 
Introducing Apache Kafka's Streams API - Kafka meetup Munich, Jan 25 2017
Introducing Apache Kafka's Streams API - Kafka meetup Munich, Jan 25 2017Introducing Apache Kafka's Streams API - Kafka meetup Munich, Jan 25 2017
Introducing Apache Kafka's Streams API - Kafka meetup Munich, Jan 25 2017
Michael Noll
 
An Introduction to Deep Learning
An Introduction to Deep LearningAn Introduction to Deep Learning
An Introduction to Deep Learning
Poo Kuan Hoong
 
Tensorflow in production with AWS Lambda
Tensorflow in production with AWS LambdaTensorflow in production with AWS Lambda
Tensorflow in production with AWS Lambda
Fabian Dubois
 
ABCs of AWS: S3
ABCs of AWS: S3ABCs of AWS: S3
ABCs of AWS: S3
Mark Cohen
 
Ad

Similar to Java 8 Lambda Expressions (20)

Intake 37 4
Intake 37 4Intake 37 4
Intake 37 4
Mahmoud Ouf
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
Ayesha Bhatti
 
Introduction to Object oriented Design
Introduction to Object oriented DesignIntroduction to Object oriented Design
Introduction to Object oriented Design
Amin Shahnazari
 
Clean Code
Clean CodeClean Code
Clean Code
Nascenia IT
 
Intake 38 5 1
Intake 38 5 1Intake 38 5 1
Intake 38 5 1
Mahmoud Ouf
 
Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4
Mahmoud Ouf
 
.Net december 2017 updates - Tamir Dresher
.Net december 2017 updates - Tamir Dresher.Net december 2017 updates - Tamir Dresher
.Net december 2017 updates - Tamir Dresher
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
Muhammed Thanveer M
 
Gg Code Mash2009 20090106
Gg Code Mash2009 20090106Gg Code Mash2009 20090106
Gg Code Mash2009 20090106
Jim Shingler
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
Codemotion
 
C# programming : Chapter One
C# programming : Chapter OneC# programming : Chapter One
C# programming : Chapter One
Khairi Aiman
 
06slide.ppt
06slide.ppt06slide.ppt
06slide.ppt
RohitNukte
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
Justin Edelson
 
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
AEM HUB
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
Sanjoy Kumar Roy
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
Maýur Chourasiya
 
Java and j2ee_lab-manual
Java and j2ee_lab-manualJava and j2ee_lab-manual
Java and j2ee_lab-manual
hanumanthu mothukuru
 
Intake 38 5
Intake 38 5Intake 38 5
Intake 38 5
Mahmoud Ouf
 
Chapter 6 Methods JAVA learning Chapter 6 Methods JAVA learning
Chapter 6 Methods JAVA learning Chapter 6 Methods JAVA learningChapter 6 Methods JAVA learning Chapter 6 Methods JAVA learning
Chapter 6 Methods JAVA learning Chapter 6 Methods JAVA learning
AhsirYu
 
05slide
05slide05slide
05slide
Dorothea Chaffin
 
Introduction to Object oriented Design
Introduction to Object oriented DesignIntroduction to Object oriented Design
Introduction to Object oriented Design
Amin Shahnazari
 
.Net december 2017 updates - Tamir Dresher
.Net december 2017 updates - Tamir Dresher.Net december 2017 updates - Tamir Dresher
.Net december 2017 updates - Tamir Dresher
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
Muhammed Thanveer M
 
Gg Code Mash2009 20090106
Gg Code Mash2009 20090106Gg Code Mash2009 20090106
Gg Code Mash2009 20090106
Jim Shingler
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
Codemotion
 
C# programming : Chapter One
C# programming : Chapter OneC# programming : Chapter One
C# programming : Chapter One
Khairi Aiman
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
Justin Edelson
 
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
AEM HUB
 
Chapter 6 Methods JAVA learning Chapter 6 Methods JAVA learning
Chapter 6 Methods JAVA learning Chapter 6 Methods JAVA learningChapter 6 Methods JAVA learning Chapter 6 Methods JAVA learning
Chapter 6 Methods JAVA learning Chapter 6 Methods JAVA learning
AhsirYu
 
Ad

More from Haim Michael (20)

Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Haim Michael
 
Introduction to Pattern Matching in Java [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]Introduction to Pattern Matching in Java [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]
Haim Michael
 
Mastering The Collections in JavaScript [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]Mastering The Collections in JavaScript [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]
Haim Michael
 
Beyond Java - Evolving to Scala and Kotlin
Beyond Java - Evolving to Scala and KotlinBeyond Java - Evolving to Scala and Kotlin
Beyond Java - Evolving to Scala and Kotlin
Haim Michael
 
JavaScript Promises Simplified [Free Meetup]
JavaScript Promises Simplified [Free Meetup]JavaScript Promises Simplified [Free Meetup]
JavaScript Promises Simplified [Free Meetup]
Haim Michael
 
Scala Jump Start [Free Online Meetup in English]
Scala Jump Start [Free Online Meetup in English]Scala Jump Start [Free Online Meetup in English]
Scala Jump Start [Free Online Meetup in English]
Haim Michael
 
The MVVM Architecture in Java [Free Meetup]
The MVVM Architecture in Java [Free Meetup]The MVVM Architecture in Java [Free Meetup]
The MVVM Architecture in Java [Free Meetup]
Haim Michael
 
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Haim Michael
 
Anti Patterns
Anti PatternsAnti Patterns
Anti Patterns
Haim Michael
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
Haim Michael
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
Haim Michael
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
Haim Michael
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in Java
Haim Michael
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
Haim Michael
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
Haim Michael
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
Haim Michael
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
Haim Michael
 
Java Jump Start
Java Jump StartJava Jump Start
Java Jump Start
Haim Michael
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
Haim Michael
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump Start
Haim Michael
 
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Haim Michael
 
Introduction to Pattern Matching in Java [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]Introduction to Pattern Matching in Java [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]
Haim Michael
 
Mastering The Collections in JavaScript [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]Mastering The Collections in JavaScript [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]
Haim Michael
 
Beyond Java - Evolving to Scala and Kotlin
Beyond Java - Evolving to Scala and KotlinBeyond Java - Evolving to Scala and Kotlin
Beyond Java - Evolving to Scala and Kotlin
Haim Michael
 
JavaScript Promises Simplified [Free Meetup]
JavaScript Promises Simplified [Free Meetup]JavaScript Promises Simplified [Free Meetup]
JavaScript Promises Simplified [Free Meetup]
Haim Michael
 
Scala Jump Start [Free Online Meetup in English]
Scala Jump Start [Free Online Meetup in English]Scala Jump Start [Free Online Meetup in English]
Scala Jump Start [Free Online Meetup in English]
Haim Michael
 
The MVVM Architecture in Java [Free Meetup]
The MVVM Architecture in Java [Free Meetup]The MVVM Architecture in Java [Free Meetup]
The MVVM Architecture in Java [Free Meetup]
Haim Michael
 
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Haim Michael
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
Haim Michael
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
Haim Michael
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
Haim Michael
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in Java
Haim Michael
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
Haim Michael
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
Haim Michael
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
Haim Michael
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
Haim Michael
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
Haim Michael
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump Start
Haim Michael
 

Recently uploaded (20)

Shift Left using Lean for Agile Software Development
Shift Left using Lean for Agile Software DevelopmentShift Left using Lean for Agile Software Development
Shift Left using Lean for Agile Software Development
SathyaShankar6
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
Agentic AI Use Cases using GenAI LLM models
Agentic AI Use Cases using GenAI LLM modelsAgentic AI Use Cases using GenAI LLM models
Agentic AI Use Cases using GenAI LLM models
Manish Chopra
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Shift Left using Lean for Agile Software Development
Shift Left using Lean for Agile Software DevelopmentShift Left using Lean for Agile Software Development
Shift Left using Lean for Agile Software Development
SathyaShankar6
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
Agentic AI Use Cases using GenAI LLM models
Agentic AI Use Cases using GenAI LLM modelsAgentic AI Use Cases using GenAI LLM models
Agentic AI Use Cases using GenAI LLM models
Manish Chopra
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 

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