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

More Related Content

What's hot (20)

PPT
Linq
Vishwa Mohan
 
PDF
Functional Programming in Java
Premanand Chandrasekaran
 
PPT
Language Integrated Query - LINQ
Doncho Minkov
 
PPT
Lexical Analyzers and Parsers
Heshan Suriyaarachchi
 
PPTX
Intro to Scala
manaswinimysore
 
PPTX
Programming Paradigm & Languages
Gaditek
 
PDF
Javanotes
John Cutajar
 
PPTX
Functional Programming In Jdk8
Bansilal Haudakari
 
PPTX
Why functional programming in C# & F#
Riccardo Terrell
 
PPTX
Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...
Simplilearn
 
PPT
C# 3.0 and LINQ Tech Talk
Michael Heydt
 
PPT
Exercises for ja se
sshhzap
 
PDF
PHP 5
Rafael Corral
 
PDF
JDT embraces lambda expressions
Eclipse Day India
 
PPT
Java Basics
shivamgarg_nitj
 
PPTX
Functional Programming in Java
Narendran Solai Sridharan
 
PPT
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA
Thuy_Dang
 
PPT
Programming In C++
shammi mehra
 
PPT
Linq in C# 3.0: An Overview
pradeepkothiyal
 
Functional Programming in Java
Premanand Chandrasekaran
 
Language Integrated Query - LINQ
Doncho Minkov
 
Lexical Analyzers and Parsers
Heshan Suriyaarachchi
 
Intro to Scala
manaswinimysore
 
Programming Paradigm & Languages
Gaditek
 
Javanotes
John Cutajar
 
Functional Programming In Jdk8
Bansilal Haudakari
 
Why functional programming in C# & F#
Riccardo Terrell
 
Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...
Simplilearn
 
C# 3.0 and LINQ Tech Talk
Michael Heydt
 
Exercises for ja se
sshhzap
 
JDT embraces lambda expressions
Eclipse Day India
 
Java Basics
shivamgarg_nitj
 
Functional Programming in Java
Narendran Solai Sridharan
 
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA
Thuy_Dang
 
Programming In C++
shammi mehra
 
Linq in C# 3.0: An Overview
pradeepkothiyal
 

Viewers also liked (6)

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

Similar to Insight into java 1.8, OOP VS FP (20)

PPT
14274730 (1).ppt
aptechaligarh
 
PPTX
Intro to java 8
John Godoi
 
PPTX
Week-1..................................
kmjanani05
 
PDF
Lambdas in Java 8
Tobias Coetzee
 
PPTX
Java 8 presentation
Van Huong
 
PPTX
Functional programming
Lhouceine OUHAMZA
 
PPTX
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
PPTX
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
PPTX
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
PPTX
java8
Arik Abulafya
 
PPTX
Lambda Expressions in Java 8
icarter09
 
PPTX
Lambdas : Beyond The Basics
Simon Ritter
 
PPTX
What's New in Java 8
javafxpert
 
PPTX
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Simon Ritter
 
PPTX
Lambdas, Collections Framework, Stream API
Prabu U
 
PPTX
Java 8 new features
Aniket Thakur
 
PDF
Fun with java 8
Victor Perepelitsky
 
PPTX
Java 8 Functional Programming - I
Ugur Yeter
 
PDF
Java 8 by example!
Mark Harrison
 
PDF
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
14274730 (1).ppt
aptechaligarh
 
Intro to java 8
John Godoi
 
Week-1..................................
kmjanani05
 
Lambdas in Java 8
Tobias Coetzee
 
Java 8 presentation
Van Huong
 
Functional programming
Lhouceine OUHAMZA
 
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
Lambda Expressions in Java 8
icarter09
 
Lambdas : Beyond The Basics
Simon Ritter
 
What's New in Java 8
javafxpert
 
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Simon Ritter
 
Lambdas, Collections Framework, Stream API
Prabu U
 
Java 8 new features
Aniket Thakur
 
Fun with java 8
Victor Perepelitsky
 
Java 8 Functional Programming - I
Ugur Yeter
 
Java 8 by example!
Mark Harrison
 
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
Ad

More from Syed Awais Mazhar Bukhari (6)

PPTX
Android App Bundles - Overview
Syed Awais Mazhar Bukhari
 
PDF
CDD - Atomic Design Methodology
Syed Awais Mazhar Bukhari
 
PDF
Introduction to Kotlin - Android KTX
Syed Awais Mazhar Bukhari
 
PDF
Intro to Rx Java
Syed Awais Mazhar Bukhari
 
PPTX
Methods of data recovery
Syed Awais Mazhar Bukhari
 
PPTX
Introduction to triggers
Syed Awais Mazhar Bukhari
 
Android App Bundles - Overview
Syed Awais Mazhar Bukhari
 
CDD - Atomic Design Methodology
Syed Awais Mazhar Bukhari
 
Introduction to Kotlin - Android KTX
Syed Awais Mazhar Bukhari
 
Intro to Rx Java
Syed Awais Mazhar Bukhari
 
Methods of data recovery
Syed Awais Mazhar Bukhari
 
Introduction to triggers
Syed Awais Mazhar Bukhari
 

Recently uploaded (20)

PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Horarios de distribución de agua en julio
pegazohn1978
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Introduction presentation of the patentbutler tool
MIPLM
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 

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 [email protected]
  • 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/

Editor's Notes

  • #24: Some addition in Collection Iterations. Foreach loop, For in loop Go for some code examples.