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

PPTX
Java 8 - An Overview
PDF
Functional programming with Java 8
PPT
Lambdas
PPTX
Introduction to functional programming with java 8
PPTX
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
PPT
C#3.0 & Vb 9.0 New Features
PPT
Understanding linq
PPTX
Java tokens
Java 8 - An Overview
Functional programming with Java 8
Lambdas
Introduction to functional programming with java 8
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
C#3.0 & Vb 9.0 New Features
Understanding linq
Java tokens

What's hot (20)

PPT
PDF
Functional Programming in Java
PPT
Language Integrated Query - LINQ
PPT
Lexical Analyzers and Parsers
PPTX
Intro to Scala
PPTX
Programming Paradigm & Languages
PDF
Javanotes
PPTX
Functional Programming In Jdk8
PPTX
Why functional programming in C# & F#
PPTX
Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...
PPT
C# 3.0 and LINQ Tech Talk
PPT
Exercises for ja se
PDF
PDF
JDT embraces lambda expressions
PPT
Java Basics
PPTX
Functional Programming in Java
PPT
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA
PPT
Programming In C++
PPT
Linq in C# 3.0: An Overview
Functional Programming in Java
Language Integrated Query - LINQ
Lexical Analyzers and Parsers
Intro to Scala
Programming Paradigm & Languages
Javanotes
Functional Programming In Jdk8
Why functional programming in C# & F#
Top TCS Interview Questions And Answers | How to Crack An Interview At TCS | ...
C# 3.0 and LINQ Tech Talk
Exercises for ja se
JDT embraces lambda expressions
Java Basics
Functional Programming in Java
Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA
Programming In C++
Linq in C# 3.0: An Overview
Ad

Viewers also liked (6)

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

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

PPT
14274730 (1).ppt
PPTX
Intro to java 8
PPTX
Week-1..................................
PDF
Lambdas in Java 8
PPTX
Java 8 presentation
PPTX
Functional programming
PPTX
Java 8 Intro - Core Features
PPTX
java150929145120-lva1-app6892 (2).pptx
PPTX
Functional Programming With Lambdas and Streams in JDK8
PPTX
PPTX
Lambda Expressions in Java 8
PPTX
Lambdas : Beyond The Basics
PPTX
What's New in Java 8
PPTX
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
PPTX
Lambdas, Collections Framework, Stream API
PPTX
Java 8 new features
PDF
Fun with java 8
PPTX
Java 8 Functional Programming - I
PDF
Java 8 by example!
PDF
Functional programming in java 8 by harmeet singh
14274730 (1).ppt
Intro to java 8
Week-1..................................
Lambdas in Java 8
Java 8 presentation
Functional programming
Java 8 Intro - Core Features
java150929145120-lva1-app6892 (2).pptx
Functional Programming With Lambdas and Streams in JDK8
Lambda Expressions in Java 8
Lambdas : Beyond The Basics
What's New in Java 8
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Lambdas, Collections Framework, Stream API
Java 8 new features
Fun with java 8
Java 8 Functional Programming - I
Java 8 by example!
Functional programming in java 8 by harmeet singh

More from Syed Awais Mazhar Bukhari (6)

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

Recently uploaded (20)

PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Computing-Curriculum for Schools in Ghana
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
Sports Quiz easy sports quiz sports quiz
Renaissance Architecture: A Journey from Faith to Humanism
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Computing-Curriculum for Schools in Ghana
STATICS OF THE RIGID BODIES Hibbelers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Anesthesia in Laparoscopic Surgery in India
102 student loan defaulters named and shamed – Is someone you know on the list?
O5-L3 Freight Transport Ops (International) V1.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Types and Its function , kingdom of life
Microbial diseases, their pathogenesis and prophylaxis
PPH.pptx obstetrics and gynecology in nursing
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Module 4: Burden of Disease Tutorial Slides S2 2025

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/

Editor's Notes

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