Top Banner
Overview of Java 8 Foundations Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA
49

Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

Jan 20, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

Overview of Java 8 Foundations

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Professor of Computer ScienceInstitute for Software Integrated Systems

Vanderbilt University Nashville, Tennessee, USA

Page 2: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

2

Learning Objectives in this Lesson• Understand key aspects of functional programming

Page 3: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

3

Learning Objectives in this Lesson• Understand key aspects of functional programming

• Contrasted with object-oriented programming

We’ll show some Java 8 code fragments that will be covered in more detail later

Page 4: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

4

Learning Objectives in this Lesson• Understand key aspects of functional programming• Recognize the benefits of applying functional

programming in Java 8

Page 5: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

5

Learning Objectives in this Lesson• Understand key aspects of functional programming• Recognize the benefits of applying functional

programming in Java 8• Especially when used in conjunction

with object-oriented programming

Again, we’ll show Java 8 code fragments that’ll be covered in more detail later

Page 6: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

6

Overview of Programming Paradigms in Java 8

Page 7: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

7See www.deadcoderising.com/why-you-should-embrace-lambdas-in-java-8

Overview of Programming Paradigms in Java 8• Java 8 is a “hybrid” that combines the object-oriented & functional paradigms

Java 8e.g., C++,

Java, C#e.g., C,

FORTRANe.g., ML,Haskell e.g., Prolog

Page 8: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

8

Overview of Programming Paradigms in Java 8• Object-oriented programming is an “imperative” paradigm

e.g., C++,Java, C#

e.g., C, FORTRAN

See en.wikipedia.org/wiki/Imperative_programming

Page 9: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

9

Overview of Programming Paradigms in Java 8• Object-oriented programming is an “imperative” paradigm

• e.g., a program consists of commands for the computer to perform

Imperative programming focuses on describing how a program operates via statements that change its state

e.g., C++,Java, C#

e.g., C, FORTRAN

Page 10: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

10

Overview of Programming Paradigms in Java 8• Object-oriented programming is an “imperative” paradigm

• e.g., a program consists of commands for the computer to performList<String> zap(List<String> lines,

String omit) { List<String> res = new ArrayList<>();

for (String line : lines) if (!omit.equals(line)) res.add(line);

return res; }

e.g., C++,Java, C#

e.g., C, FORTRAN

Imperatively remove a designated string from a list of strings

Page 11: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

11

Overview of Programming Paradigms in Java 8• Object-oriented programming is an “imperative” paradigm

• e.g., a program consists of commands for the computer to performList<String> zap(List<String> lines,

String omit) { List<String> res = new ArrayList<>();

for (String line : lines) if (!omit.equals(line)) res.add(line);

return res; }

e.g., C++,Java, C#

e.g., C, FORTRAN

See www.ibm.com/developerworks/library/j-java-streams-2-brian-goetz

This inherently sequential code applies the Accumulator anti-pattern

Page 12: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

12See en.wikipedia.org/wiki/Declarative_programming

Overview of Programming Paradigms in Java 8• Conversely, functional programming is a “declarative” paradigm

e.g., Prologe.g., ML,Haskell

Page 13: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

13

Overview of Programming Paradigms in Java 8• Conversely, functional programming is a “declarative” paradigm

• e.g., a program expresses computational logic without describing control flow or explicit algorithmic steps

Declarative programming focuses on “what” computations to perform, not “how” to compute them

e.g., Prologe.g., ML,Haskell

Page 14: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

14

Overview of Programming Paradigms in Java 8• Conversely, functional programming is a “declarative” paradigm

• e.g., a program expresses computational logic without describing control flow or explicit algorithmic steps

Declaratively remove a designated string from a list of strings

List<String> zap(List<String> lines, String omit) {

return lines.stream().filter(not(omit::equals)).collect(toList());

} e.g., Prologe.g., ML,Haskell

See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex0

Page 15: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

15

Overview of Programming Paradigms in Java 8• Conversely, functional programming is a “declarative” paradigm

• e.g., a program expresses computational logic without describing control flow or explicit algorithmic steps

Note “fluent” programming style with cascading method calls

List<String> zap(List<String> lines, String omit) {

return lines.stream().filter(not(omit::equals)).collect(toList());

} e.g., Prologe.g., ML,Haskell

See en.wikipedia.org/wiki/Fluent_interface

Page 16: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

16

Overview of Programming Paradigms in Java 8• Conversely, functional programming is a “declarative” paradigm

• e.g., a program expresses computational logic without describing control flow or explicit algorithmic stepsList<String> zap(List<String> lines,

String omit) { return lines.parallelStream().filter(not(omit::equals)).collect(toList());

} e.g., Prologe.g., ML,Haskell

Filter in parallel

See docs.oracle.com/javase/tutorial/collections/streams/parallelism.html

Page 17: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

17Code was parallelized with minuscule changes since it’s declarative & stateless!

Overview of Programming Paradigms in Java 8• Conversely, functional programming is a “declarative” paradigm

• e.g., a program expresses computational logic without describing control flow or explicit algorithmic stepsList<String> zap(List<String> lines,

String omit) { return lines.parallelStream().filter(not(omit::equals)).collect(toList());

} e.g., Prologe.g., ML,Haskell

Filter in parallel

Page 18: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

18

Overview of Functional Programming in Java 8

Page 19: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

19See en.wikipedia.org/wiki/Functional_programming

Overview of Functional Programming in Java 8• Functional programming has its roots

in lambda calculus

Page 20: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

20

Overview of Functional Programming in Java 8• Functional programming has its roots

in lambda calculus, e.g.,• Computations are treated as the

evaluation of mathematical functionsFunction f:

Function g:

Function h:

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

See en.wikipedia.org/wiki/Functional_programming#Pure_functions

Note “function composition”: the output of one function serves as

the input to the next function, etc.

Page 21: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

21

Overview of Functional Programming in Java 8• Functional programming has its roots

in lambda calculus, e.g.,• Computations are treated as the

evaluation of mathematical functionsFunction f:

Function g:

Function h:

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

long factorial(long n) {return LongStream.rangeClosed(1, n).parallel().reduce(1, (a, b) -> a * b);

}

See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex16

Page 22: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

22

Overview of Functional Programming in Java 8• Functional programming has its roots

in lambda calculus, e.g.,• Computations are treated as the

evaluation of mathematical functions• Changing state & mutable data are

discouraged to avoid various hazards

See en.wikipedia.org/wiki/Side_effect_(computer_science)

Page 23: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

23

class Total {public long mTotal = 1;

public void mult(long n) { mTotal *= n; }

}

See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex16

Overview of Functional Programming in Java 8• Functional programming has its roots

in lambda calculus, e.g.,• Computations are treated as the

evaluation of mathematical functions• Changing state & mutable data are

discouraged to avoid various hazardslong factorial(long n) {

Total t = new Total();LongStream.rangeClosed(1, n)

.parallel()

.forEach(t::mult);return t.mTotal;

}

Shared mutable state

Page 24: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

24

Overview of Functional Programming in Java 8• Functional programming has its roots

in lambda calculus, e.g.,• Computations are treated as the

evaluation of mathematical functions• Changing state & mutable data are

discouraged to avoid various hazardslong factorial(long n) {

Total t = new Total();LongStream.rangeClosed(1, n)

.parallel()

.forEach(t::mult);return t.mTotal;

}

class Total {public long mTotal = 1;

public void mult(long n) { mTotal *= n; }

}

Beware of race conditions!!!

See en.wikipedia.org/wiki/Race_condition#Software

Page 25: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

25See jeremymanson.blogspot.com/2007/08/atomicity-visibility-and-ordering.html

Overview of Functional Programming in Java 8• Functional programming has its roots

in lambda calculus, e.g.,• Computations are treated as the

evaluation of mathematical functions• Changing state & mutable data are

discouraged to avoid various hazardslong factorial(long n) {

Total t = new Total();LongStream.rangeClosed(1, n)

.parallel()

.forEach(t::mult);return t.mTotal;

}

class Total {public long mTotal = 1;

public void mult(long n) { mTotal *= n; }

}

Beware of inconsistent memory visibility

Page 26: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

26In Java you must avoid these hazards, i.e., the compiler & JVM won’t save you..

Overview of Functional Programming in Java 8• Functional programming has its roots

in lambda calculus, e.g.,• Computations are treated as the

evaluation of mathematical functions• Changing state & mutable data are

discouraged to avoid various hazardslong factorial(long n) {

Total t = new Total();LongStream.rangeClosed(1, n)

.parallel()

.forEach(t::mult);return t.mTotal;

}

class Total {public long mTotal = 1;

public void mult(long n) { mTotal *= n; }

}

Only you can prevent concurrency hazards!

Page 27: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

27See docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html

Overview of Functional Programming in Java 8• Functional programming has its roots

in lambda calculus, e.g.,• Computations are treated as the

evaluation of mathematical functions• Changing state & mutable data are

discouraged to avoid various hazards• Instead, the focus is on “immutable”

objects• i.e., objects whose state cannot

change after they are constructed

Page 28: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

28See en.wikipedia.org/wiki/Object-oriented_design

Overview of Functional Programming in Java 8• In contrast, object-oriented programming

employs “hierarchical data abstraction”

Page 29: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

29

Overview of Functional Programming in Java 8• In contrast, object-oriented programming

employs “hierarchical data abstraction”, e.g.• Components are based on stable class

roles & relationships extensible via inheritance & dynamic binding

See en.wikipedia.org/wiki/Object-oriented_programming

Page 30: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

30

Overview of Functional Programming in Java 8• In contrast, object-oriented programming

employs “hierarchical data abstraction”, e.g.• Components are based on stable class

roles & relationships extensible via inheritance & dynamic binding• Rather than by functions that

correspond to algorithmic actions

See www.drdobbs.com/windows/software-complexity-bringing-order-to-ch/199901062

Page 31: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

31

Overview of Functional Programming in Java 8• In contrast, object-oriented programming

employs “hierarchical data abstraction”, e.g.• Components are based on stable class

roles & relationships extensible via inheritance & dynamic binding

• State is encapsulated by methodsthat perform imperative statements

See en.wikipedia.org/wiki/Imperative_programming

Tree tree = ...;Visitor printVisitor =

makeVisitor(...);

for(Iterator<Tree> iter = tree.iterator();

iter.hasNext();)iter.next().accept(printVisitor);

Page 32: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

32

Overview of Functional Programming in Java 8• In contrast, object-oriented programming

employs “hierarchical data abstraction”, e.g.• Components are based on stable class

roles & relationships extensible via inheritance & dynamic binding

• State is encapsulated by methodsthat perform imperative statements• This state is often mutable

See en.wikipedia.org/wiki/Imperative_programming

Tree tree = ...;Visitor printVisitor =

makeVisitor(...);

for(Iterator<Tree> iter = tree.iterator();

iter.hasNext();)iter.next().accept(printVisitor);

Access & update internal state of the iterator

Page 33: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

33

Combining Object-Oriented (OO) & Functional

Programming (FP) in Java 8

Page 34: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

34

Benefits of Combining OO & FP in Java 8• Java 8’s combination of functional & object-oriented

paradigms is powerful!

Java 8

e.g., C++,Java, C#

e.g., C, FORTRAN

e.g., ML,Haskell e.g., Prolog

Page 35: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

35

Benefits of Combining OO & FP in Java 8• Java 8’s functional features help close the gap between a program’s “domain

intent” & its computations

See www.toptal.com/software/declarative-programming

Page 36: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

36

Socket

Socket

Benefits of Combining OO & FP in Java 8• Java 8’s functional features help close the gap between a program’s “domain

intent” & its computations, e.g.,• Domain intent defines “what”

Download images that aren’t already cached from a list of URLs & process/store the images in parallel

See github.com/douglascraigschmidt/LiveLessons/tree/master/ImageStreamGang

Page 37: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

37

Benefits of Combining OO & FP in Java 8• Java 8’s functional features help close the gap between a program’s “domain

intent” & its computations, e.g.,• Domain intent defines “what”• Computations define “how”

See github.com/douglascraigschmidt/LiveLessons/tree/master/ImageStreamGang

List<Image> images = urls.parallelStream().filter(not(this::urlCached)).map(this::downloadImage).flatMap(this::applyFilters).collect(toList());

Download images that aren’t already cached from a list of URLs & process/store the images in parallel

Socket

Socket

Page 38: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

38

• Java 8’s functional features help close the gap between a program’s “domainintent” & its computations, e.g.,• Domain intent defines “what”• Computations define “how”

Benefits of Combining OO & FP in Java 8

Java 8 functional programming features connect domain intent & computations

List<Image> images = urls.parallelStream().filter(not(this::urlCached)).map(this::downloadImage).flatMap(this::applyFilters).collect(toList());

Socket

Socket

Page 39: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

39

Benefits of Combining OO & FP in Java 8• Likewise, Java 8’s object-oriented features help to structure a program’s

software architecture

See en.wikipedia.org/wiki/Software_architecture

LogicalView

PhysicalView

DevelopmentView

ProcessView

Use CaseView

Page 40: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

40

Benefits of Combining OO & FP in Java 8• Likewise, Java 8’s object-oriented features help to structure a program’s

software architecture

See sce.uhcl.edu/helm/rationalunifiedprocess/process/workflow/ana_desi/co_lview.htm

LogicalView

DevelopmentView

ProcessView

Use CaseView

Depicts key use-case realizations, subsystems, packages, & classes that encompass architecturally

significant behavior

Page 41: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

41

Benefits of Combining OO & FP in Java 8• e.g., consider the ImageStreamGang program

See github.com/douglascraigschmidt/LiveLessons/tree/master/ImageStreamGang

Page 42: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

42

Benefits of Combining OO & FP in Java 8• e.g., consider the ImageStreamGang program

• Common classes provide a reusable foundation for extensibility

See www.dre.vanderbilt.edu/~schmidt/PDF/Commonality_Variability.pdf

Page 43: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

43

Benefits of Combining OO & FP in Java 8• e.g., consider the ImageStreamGang program

• Common classes provide a reusable foundation for extensibility

• Subclasses extend the commonclasses to create various customimplementation strategies

See www.dre.vanderbilt.edu/~schmidt/PDF/Commonality_Variability.pdf

Page 44: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

44

Benefits of Combining OO & FP in Java 8

See www.drdobbs.com/jvm/lambda-expressions-in-java-8/240166764

• e.g., consider the ImageStreamGang program• Common classes provide a reusable

foundation for extensibility• Subclasses extend the common

classes to create various customimplementation strategies

• Java 8’s FP features are most effective when used to simplify computations within the context of an OO software architecture

List<Image> images = urls.parallelStream()

.filter(not(this::urlCached)).map(this::downloadImage).flatMap(this::applyFilters).collect(toList());

Page 45: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

45

Benefits of Combining OO & FP in Java 8

See docs.oracle.com/javase/tutorial/collections/streams/parallelism.html

• e.g., consider the ImageStreamGang program• Common classes provide a reusable

foundation for extensibility• Subclasses extend the common

classes to create various customimplementation strategies

• Java 8’s FP features are most effective when used to simplify computations within the context of an OO software architecture• Especially concurrent

& parallel computations

List<Image> images = urls.parallelStream()

.filter(not(this::urlCached)).map(this::downloadImage).flatMap(this::applyFilters).collect(toList());

Page 46: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

46

Benefits of Combining OO & FP in Java 8

See www.infoq.com/articles/How-Functional-is-Java-8

• Since Java 8 is a hybrid language, there are situations in which mutable changes to state are allowed/encouraged

Page 47: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

47

Benefits of Combining OO & FP in Java 8• Since Java 8 is a hybrid language, there are situations in which mutable

changes to state are allowed/encouraged• e.g., Java collection

framework classes

See docs.oracle.com/javase/8/docs/technotes/guides/collections

Page 48: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

48

Benefits of Combining OO & FP in Java 8• However, you’re usually better off by

minimizing/avoiding the use of shared mutable state in your programs!!

Page 49: Overview of Java 8 Foundationsschmidt/cs891f/2018-PDFs/01-overview-of-Java-8-foundations.pdfOverview of Programming Paradigms in Java 8 • Object-oriented programming is an “imperative”

49

End of Overview of Java 8 Foundations