SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Insight into Java 1.8
“Java is still not dead—and people are starting to figure that out.”
Prepared by: Syed Awais Mazhar
awais.mazhar@mobileive.ca
Agenda
Object Oriented Programming.
Functional Programming.
FP VS OOP
Java 8 Recent API’s
5 mins
Object Oriented
Programming.
5 mins
Functional
Programming.
5 mins
FP VS OOP
35 mins
Java 8 Recent API’s
- Lambda Expressions
- Method References
- Built-in Functional
Interfaces, etc.
15 mins
Java Collections and
Streams
Time Breakthrough
Object Oriented Programming
OOP is a programming paradigm based on the concept of "objects", which are data
structures that contain data, in the form of fields, often known as attributes; and code, in the
form of procedures, often known as methods.
When to choose object oriented approach?
When you have a fixed set of operations on things, and as your code evolves, you primarily
add new things. This can be accomplished by adding new classes which implement existing
methods, and the existing classes are left alone.
Functional Programming
Functional programming is a programming paradigm, a style of building the structure and
elements of computer programs, that treats computation as the evaluation of mathematical
functions and avoids changing-state and mutable data.
When to choose functional approach?
When you have a fixed set of things, and as your code evolves, you primarily add new
operations on existing things. This can be accomplished by adding new functions which
compute with existing data types, and the existing functions are left alone.
FP says that data and behavior
are distinctively different things
and should be kept separate
for clarity
Data is only loosely coupled to
functions.
Functions hide implementations
OOP says that bringing together
data and its behaviour in a
single location makes it easier
to understand how a program
works.
Data and operations are tightly
coupled.
Objects hide implementation of
operations from other objects.
Object Oriented Programming Functional Programming
VS
Central model of abstraction is
the function, not the data
structure
Central activity is writing new
functions, and composing
functions together.
Central model of abstraction is
the data itself.
Central activity is composing new
objects and extending existing
objects by adding new
methods
Object Oriented Programming Functional Programming
VS
Java 8
Default Methods for Interfaces
Functional Interfaces
Lambda expressions
Method References
Built-in Functional Interfaces
Streams
Why Lambdas?
Default Methods for Interfaces
Java 8 enables us to add non-abstract method implementations to interfaces by utilizing the
default keyword.
Functional Interfaces
An interface which contain exactly one abstract method declaration. Since default methods
are not abstract you're free to add default methods to your functional interface.
To ensure that your interface meet the requirements, you should add the @FunctionalInterface
annotation. The compiler is aware of this annotation and throws a compiler error as soon as
you try to add a second abstract method declaration to the interface.
Lambda - a formal definition
Anonymous function (also function literal or lambda abstraction) is a function definition that
is not bound to an identifier. Lambdas are often:
● Passed as an arguments to higher order functions.
Or
● Used to construct a result of a higher order function that needs to return a function.
Lambda expressions
Let's start with a simple example of how to sort a list of strings in prior versions of Java:
The static utility method Collections.sort accepts a list and a comparator in order to sort the
elements of the given list. You often find yourself creating anonymous comparators and
pass them to the sort method.
Lambda expressions
Instead of creating anonymous objects all day long, Java 8 comes with a much shorter
syntax, lambda expressions:
For one line method bodies you can skip both the braces {} and the return keyword. The java
compiler is aware of the parameter types so you can skip them as well.
Lambda Scopes
Accessing outer scope variables from lambda expressions is very similar to anonymous
objects. You can access final variables from the local outer scope as well as instance fields
and static variables.
But different to anonymous objects the variable num does not have to be declared final.
But It is meant to be final, i.e we can not change the value of num.
It will be compile time error if we do so.
Lambda Scopes
Accessing Default Interface Methods:
Default methods cannot be accessed from within lambda expressions. The following code
does not compile:
Method References
It is a feature which is related to Lambda Expression. It allows us to reference constructors
or methods without executing them. Method references and Lambda are similar in that they
both require a target type that consist of a compatible functional interface.
Built-in Functional Interfaces
The JDK 1.8 API contains many built-in functional interfaces. Some of them are well known
from older versions of Java like Comparator or Runnable. Those existing interfaces are extended
to enable Lambda support via the @FunctionalInterface annotation.
Predicates:
Predicates are boolean-valued functions of one argument. The interface contains various
default methods for composing predicates to complex logical terms (and, or, negate)
Built-in Functional Interfaces
Predicates:
Built-in Functional Interfaces
Functions:
Functions accept one argument and produce a result. Default methods can be used to chain
multiple functions together (compose, andThen).
Built-in Functional Interfaces
Suppliers:
Suppliers produce a result of a given generic type. Unlike Functions, Suppliers don't accept
arguments.
Consumers:
Consumers represents operations to be performed on a single input argument.
Built-in Functional Interfaces
Optionals:
Optional is a simple container for a value which may be null or non-null. Think of a method
which may return a non-null result but sometimes return nothing. Instead of returning null
you return an Optional in Java 8.
Streams
A java.util.Stream represents a sequence of elements on which one or more operations can
be performed. Stream operations are either intermediate or terminal. While terminal
operations return a result of a certain type, intermediate operations return the stream itself
so you can chain multiple method calls in a row. Streams are created on a source, e.g. a
java.util.Collection like lists or sets (maps are not supported).
Stream operations can either be executed sequential or parallel.
Streams
Collections in Java 8 are extended so you can simply create streams either by calling
Collection.stream() or Collection.parallelStream() . The following sections explain the most
common stream operations.
I. Filter II. Sorted
III. Map IV. Match
V. Count VI. Reduce
Parallel Streams
As mentioned above streams can be either sequential or parallel. Operations on sequential
streams are performed on a single thread while operations on parallel streams are
performed concurrently on multiple threads.
Let’s check the code example.
Lambda expressions
Why Lambdas?
Enables Functional Programming
Readable and concise code.
Easier to use API’s and Libraries.
Enable Support for parallel processing
References
1. https://www.slideshare.net/LivePersonDev/functional-programming-with-java-8
2. http://www.cogsys.wiai.uni-bamberg.de/schmid/uoshp/lehreuos/fp01-www/fp-referate/oo-vs-fp.pdf
3. http://stackoverflow.com/questions/2078978/functional-programming-vs-object-oriented-
programming
4. https://hackpad.com/ep/pad/static/x4m38aCcrMs
5. http://winterbe.com/posts/2014/03/16/java-8-tutorial/
6. https://blog.idrsolutions.com/2015/02/java-8-method-references-explained-5-minutes/

Weitere ähnliche Inhalte

Was ist angesagt?

Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQDoncho Minkov
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & LanguagesGaditek
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8 Bansilal Haudakari
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#Riccardo Terrell
 
Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...
Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...
Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...Simplilearn
 
C# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech TalkC# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech TalkMichael Heydt
 
Exercises for ja se
Exercises for ja seExercises for ja se
Exercises for ja sesshhzap
 
JDT embraces lambda expressions
JDT embraces lambda expressionsJDT embraces lambda expressions
JDT embraces lambda expressionsEclipse Day India
 
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEAScala - By Luu Thanh Thuy CWI team from eXo Platform SEA
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEAThuy_Dang
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++ shammi mehra
 

Was ist angesagt? (20)

Java tokens
Java tokensJava tokens
Java tokens
 
Linq
LinqLinq
Linq
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQ
 
Lexical Analyzers and Parsers
Lexical Analyzers and ParsersLexical Analyzers and Parsers
Lexical Analyzers and Parsers
 
Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Javanotes
JavanotesJavanotes
Javanotes
 
LINQ
LINQLINQ
LINQ
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
 
Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...
Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...
Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...
 
C# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech TalkC# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech Talk
 
Exercises for ja se
Exercises for ja seExercises for ja se
Exercises for ja se
 
PHP 5
PHP 5PHP 5
PHP 5
 
JDT embraces lambda expressions
JDT embraces lambda expressionsJDT embraces lambda expressions
JDT embraces lambda expressions
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEAScala - By Luu Thanh Thuy CWI team from eXo Platform SEA
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++
 

Andere mochten auch

Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional ProgrammingJordan Parmer
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersDiego Freniche Brito
 
Павел Павлов - Scala для профессионалов - Joker 2013
Павел Павлов - Scala для профессионалов - Joker 2013Павел Павлов - Scala для профессионалов - Joker 2013
Павел Павлов - Scala для профессионалов - Joker 2013ScalaNsk
 
Функциональное программирование: мифы и реальность
Функциональное программирование: мифы и реальностьФункциональное программирование: мифы и реальность
Функциональное программирование: мифы и реальностьCUSTIS
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
Quantum computing - Introduction
Quantum computing - IntroductionQuantum computing - Introduction
Quantum computing - Introductionrushmila
 

Andere mochten auch (6)

Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional Programming
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented Programmers
 
Павел Павлов - Scala для профессионалов - Joker 2013
Павел Павлов - Scala для профессионалов - Joker 2013Павел Павлов - Scala для профессионалов - Joker 2013
Павел Павлов - Scala для профессионалов - Joker 2013
 
Функциональное программирование: мифы и реальность
Функциональное программирование: мифы и реальностьФункциональное программирование: мифы и реальность
Функциональное программирование: мифы и реальность
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Quantum computing - Introduction
Quantum computing - IntroductionQuantum computing - Introduction
Quantum computing - Introduction
 

Ähnlich wie Insight into java 1.8, OOP VS FP

Week-1..................................
Week-1..................................Week-1..................................
Week-1..................................kmjanani05
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhHarmeet Singh(Taara)
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features OverviewSergii Stets
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsEmiel Paasschens
 
New Features of JAVA SE8
New Features of JAVA SE8New Features of JAVA SE8
New Features of JAVA SE8Dinesh Pathak
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APIPrabu U
 
Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Noopur Gupta
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptxSameerAhmed593310
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript FundamentalsSrdjan Strbanovic
 
Example Of Import Java
Example Of Import JavaExample Of Import Java
Example Of Import JavaMelody Rios
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesRaffi Khatchadourian
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usageAsmaShaikh478737
 
Functional Interfaces and Method References.pptx
Functional Interfaces and Method References.pptxFunctional Interfaces and Method References.pptx
Functional Interfaces and Method References.pptxMuhammadSalman701062
 

Ähnlich wie Insight into java 1.8, OOP VS FP (20)

Week-1..................................
Week-1..................................Week-1..................................
Week-1..................................
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features Overview
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
New Features of JAVA SE8
New Features of JAVA SE8New Features of JAVA SE8
New Features of JAVA SE8
 
Java 8-revealed
Java 8-revealedJava 8-revealed
Java 8-revealed
 
Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream API
 
14274730 (1).ppt
14274730 (1).ppt14274730 (1).ppt
14274730 (1).ppt
 
Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013
 
Smart Migration to JDK 8
Smart Migration to JDK 8Smart Migration to JDK 8
Smart Migration to JDK 8
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptx
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
 
Example Of Import Java
Example Of Import JavaExample Of Import Java
Example Of Import Java
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to Interfaces
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usage
 
Java_Interview Qns
Java_Interview QnsJava_Interview Qns
Java_Interview Qns
 
Java 8 Overview
Java 8 OverviewJava 8 Overview
Java 8 Overview
 
Functional Interfaces and Method References.pptx
Functional Interfaces and Method References.pptxFunctional Interfaces and Method References.pptx
Functional Interfaces and Method References.pptx
 

Mehr von Syed Awais Mazhar Bukhari (6)

Android App Bundles - Overview
Android App Bundles - OverviewAndroid App Bundles - Overview
Android App Bundles - Overview
 
CDD - Atomic Design Methodology
CDD - Atomic Design MethodologyCDD - Atomic Design Methodology
CDD - Atomic Design Methodology
 
Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
Intro to Rx Java
Intro to Rx JavaIntro to Rx Java
Intro to Rx Java
 
Methods of data recovery
Methods of data recoveryMethods of data recovery
Methods of data recovery
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 

Kürzlich hochgeladen

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Kürzlich hochgeladen (20)

YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 

Insight into java 1.8, OOP VS FP

  • 1. Insight into Java 1.8 “Java is still not dead—and people are starting to figure that out.” Prepared by: Syed Awais Mazhar awais.mazhar@mobileive.ca
  • 2. Agenda Object Oriented Programming. Functional Programming. FP VS OOP Java 8 Recent API’s
  • 3. 5 mins Object Oriented Programming. 5 mins Functional Programming. 5 mins FP VS OOP 35 mins Java 8 Recent API’s - Lambda Expressions - Method References - Built-in Functional Interfaces, etc. 15 mins Java Collections and Streams Time Breakthrough
  • 4. Object Oriented Programming OOP is a programming paradigm based on the concept of "objects", which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. When to choose object oriented approach? When you have a fixed set of operations on things, and as your code evolves, you primarily add new things. This can be accomplished by adding new classes which implement existing methods, and the existing classes are left alone.
  • 5. Functional Programming Functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. When to choose functional approach? When you have a fixed set of things, and as your code evolves, you primarily add new operations on existing things. This can be accomplished by adding new functions which compute with existing data types, and the existing functions are left alone.
  • 6. FP says that data and behavior are distinctively different things and should be kept separate for clarity Data is only loosely coupled to functions. Functions hide implementations OOP says that bringing together data and its behaviour in a single location makes it easier to understand how a program works. Data and operations are tightly coupled. Objects hide implementation of operations from other objects. Object Oriented Programming Functional Programming VS
  • 7. Central model of abstraction is the function, not the data structure Central activity is writing new functions, and composing functions together. Central model of abstraction is the data itself. Central activity is composing new objects and extending existing objects by adding new methods Object Oriented Programming Functional Programming VS
  • 8. Java 8 Default Methods for Interfaces Functional Interfaces Lambda expressions Method References Built-in Functional Interfaces Streams Why Lambdas?
  • 9. Default Methods for Interfaces Java 8 enables us to add non-abstract method implementations to interfaces by utilizing the default keyword.
  • 10. Functional Interfaces An interface which contain exactly one abstract method declaration. Since default methods are not abstract you're free to add default methods to your functional interface. To ensure that your interface meet the requirements, you should add the @FunctionalInterface annotation. The compiler is aware of this annotation and throws a compiler error as soon as you try to add a second abstract method declaration to the interface.
  • 11. Lambda - a formal definition Anonymous function (also function literal or lambda abstraction) is a function definition that is not bound to an identifier. Lambdas are often: ● Passed as an arguments to higher order functions. Or ● Used to construct a result of a higher order function that needs to return a function.
  • 12. Lambda expressions Let's start with a simple example of how to sort a list of strings in prior versions of Java: The static utility method Collections.sort accepts a list and a comparator in order to sort the elements of the given list. You often find yourself creating anonymous comparators and pass them to the sort method.
  • 13. Lambda expressions Instead of creating anonymous objects all day long, Java 8 comes with a much shorter syntax, lambda expressions: For one line method bodies you can skip both the braces {} and the return keyword. The java compiler is aware of the parameter types so you can skip them as well.
  • 14. Lambda Scopes Accessing outer scope variables from lambda expressions is very similar to anonymous objects. You can access final variables from the local outer scope as well as instance fields and static variables. But different to anonymous objects the variable num does not have to be declared final. But It is meant to be final, i.e we can not change the value of num. It will be compile time error if we do so.
  • 15. Lambda Scopes Accessing Default Interface Methods: Default methods cannot be accessed from within lambda expressions. The following code does not compile:
  • 16. Method References It is a feature which is related to Lambda Expression. It allows us to reference constructors or methods without executing them. Method references and Lambda are similar in that they both require a target type that consist of a compatible functional interface.
  • 17. Built-in Functional Interfaces The JDK 1.8 API contains many built-in functional interfaces. Some of them are well known from older versions of Java like Comparator or Runnable. Those existing interfaces are extended to enable Lambda support via the @FunctionalInterface annotation. Predicates: Predicates are boolean-valued functions of one argument. The interface contains various default methods for composing predicates to complex logical terms (and, or, negate)
  • 19. Built-in Functional Interfaces Functions: Functions accept one argument and produce a result. Default methods can be used to chain multiple functions together (compose, andThen).
  • 20. Built-in Functional Interfaces Suppliers: Suppliers produce a result of a given generic type. Unlike Functions, Suppliers don't accept arguments. Consumers: Consumers represents operations to be performed on a single input argument.
  • 21. Built-in Functional Interfaces Optionals: Optional is a simple container for a value which may be null or non-null. Think of a method which may return a non-null result but sometimes return nothing. Instead of returning null you return an Optional in Java 8.
  • 22. Streams A java.util.Stream represents a sequence of elements on which one or more operations can be performed. Stream operations are either intermediate or terminal. While terminal operations return a result of a certain type, intermediate operations return the stream itself so you can chain multiple method calls in a row. Streams are created on a source, e.g. a java.util.Collection like lists or sets (maps are not supported). Stream operations can either be executed sequential or parallel.
  • 23. Streams Collections in Java 8 are extended so you can simply create streams either by calling Collection.stream() or Collection.parallelStream() . The following sections explain the most common stream operations. I. Filter II. Sorted III. Map IV. Match V. Count VI. Reduce
  • 24. Parallel Streams As mentioned above streams can be either sequential or parallel. Operations on sequential streams are performed on a single thread while operations on parallel streams are performed concurrently on multiple threads. Let’s check the code example.
  • 25. Lambda expressions Why Lambdas? Enables Functional Programming Readable and concise code. Easier to use API’s and Libraries. Enable Support for parallel processing
  • 26. References 1. https://www.slideshare.net/LivePersonDev/functional-programming-with-java-8 2. http://www.cogsys.wiai.uni-bamberg.de/schmid/uoshp/lehreuos/fp01-www/fp-referate/oo-vs-fp.pdf 3. http://stackoverflow.com/questions/2078978/functional-programming-vs-object-oriented- programming 4. https://hackpad.com/ep/pad/static/x4m38aCcrMs 5. http://winterbe.com/posts/2014/03/16/java-8-tutorial/ 6. https://blog.idrsolutions.com/2015/02/java-8-method-references-explained-5-minutes/

Hinweis der Redaktion

  1. Some addition in Collection Iterations. Foreach loop, For in loop Go for some code examples.