java streams: overview & benefits · 3 learning objectives in this part of the lesson...

23
Java Streams: Overview & Benefits Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Upload: others

Post on 20-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

Java Streams: Overview & Benefits

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Professor of Computer Science

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

2

Learning Objectives in this Part of the Lesson• Understand the structure & functionality

of Java streamsStream source

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Aggregate operation (behavior h)

Input x

Output f(x)

Output g(f(x))

Page 3: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

3

Learning Objectives in this Part of the Lesson• Understand the structure & functionality

of Java streams, e.g.,

• Fundamentals of streams

Stream source

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Aggregate operation (behavior h)

Input x

Output f(x)

Output g(f(x))

Page 4: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

4

Learning Objectives in this Part of the Lesson• Understand the structure & functionality

of Java streams, e.g.,

• Fundamentals of streams

• Benefits of streams

Stream source

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Aggregate operation (behavior h)

Input x

Output f(x)

Output g(f(x))

Page 5: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

5

Overview of Java Streams

Page 6: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

6

• Java streams are a framework firstintroduced into the Java class libraryin Java 8

Overview of Java Streams

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

Page 7: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

7

• Java streams are a framework firstintroduced into the Java class libraryin Java 8

• They have continued to evolve a bit in later versions of Java

Overview of Java Streams

See www.baeldung.com/java-9-stream-api & blog.codefx.org/java/teeing-collector

Page 8: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

8

• A stream is a pipeline of aggregate operations that process a sequence of elements (aka, “values” or “data”)

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Aggregate operation (behavior h)

Input x

Output f(x)

Output g(f(x))

Overview of Java Streams

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

Page 9: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

9See en.wikipedia.org/wiki/Higher-order_function

• A stream is a pipeline of aggregate operations that process a sequence of elements (aka, “values” or “data”)

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Aggregate operation (behavior h)

Input x

Output f(x)

Output g(f(x))

Overview of Java Streams

An aggregate operation is a higher-order function that applies a “behavior”

param to every element in a stream.

Page 10: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

10

Behavioral parameterization simplifies coping with changing requirements.

See blog.indrek.io/articles/java-8-behavior-parameterization

• A stream is a pipeline of aggregate operations that process a sequence of elements (aka, “values” or “data”)

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Aggregate operation (behavior h)

Input x

Output f(x)

Output g(f(x))

Overview of Java Streams

Page 11: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

11

• A stream is a pipeline of aggregate operations that process a sequence of elements (aka, “values” or “data”)

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Aggregate operation (behavior h)

Input x

Output f(x)

Output g(f(x))

A stream is conceptually unbounded, though it’s often bounded by practical constraints.

Overview of Java Streams

Page 12: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

12

• We use this stream as a case study example throughout this introduction

Aggregate operation (behavior f)

Aggregate operation (behavior g)

Aggregate operation (behavior h)

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

Input x

Output f(x)

Output g(f(x))

Stream

.of("Ophelia","horatio",

"laertes","Gertrude",

"Hamlet","fortinbras", ...)

.filter(s -> toLowerCase

(s.charAt(0)) == 'h')

.map(this::capitalize)

.sorted()

.forEach(System.out::println);

Overview of Java Streams

Print each character in Hamlet that starts with ‘H’ or ‘h’ in consistently capitalized & sorted order.

Page 13: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

13

Benefits of Java Streams

Page 14: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

14

• Java streams provide several key benefits to programs & programmers

Benefits of Java Streams…

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

stream()

Page 15: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

15

• Java streams provide several key benefits to programs & programmers

Benefits of Java Streams…

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

stream()

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

SocketSocket

List of URLs to Download

List of Filters to Apply

Persistent

Data Store

Page 16: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

16

• Java streams provide several key benefits to programs & programmers, e.g.

• Concise & readable

• Declarative paradigm focuses on whatfunctions to perform, not how to

perform them

Benefits of Java Streams…

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

stream()

See en.wikipedia.org/wiki/Declarative_programming

Page 17: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

17

• Java streams provide several key benefits to programs & programmers, e.g.

• Concise & readable

• Declarative paradigm focuses on whatfunctions to perform, not how to

perform them

Benefits of Java Streams…

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

stream()

e.g., no Java control-flow operations are

applied in this stream

See docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html

Page 18: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

18

• Java streams provide several key benefits to programs & programmers, e.g.

• Concise & readable

• Flexible & composable

• Functions are automatically composed together

Benefits of Java Streams…

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

stream()

Page 19: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

19

• Java streams provide several key benefits to programs & programmers, e.g.

• Concise & readable

• Flexible & composable

• Functions are automatically composed together

Benefits of Java Streams…

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

stream()

e.g., the output from filter() is passed as the input to map() etc.

Page 20: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

20

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

parallelStream()

• Java streams provide several key benefits to programs & programmers, e.g.

• Concise & readable

• Flexible & composable

• Scalable

• Parallelize performance without the need to write any multi-threaded code

Benefits of Java Streams

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

Page 21: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

21

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

parallelStream()

• Java streams provide several key benefits to programs & programmers, e.g.

• Concise & readable

• Flexible & composable

• Scalable

• Parallelize performance without the need to write any multi-threaded code

Benefits of Java Streams

See dzone.com/articles/common-fork-join-pool-and-streams

A pool of worker threads is used to process behaviors in parallel

Page 22: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

22

filter(not(this::urlCached))

collect(toList())

map(this::downloadImage)

flatMap(this::applyFilters)

parallelStream()

• Java streams provide several key benefits to programs & programmers, e.g.

• Concise & readable

• Flexible & composable

• Scalable

• Parallelize performance without the need to write any multi-threaded code

Benefits of Java Streams

See gee.cs.oswego.edu/dl/papers/fj.pdf

Data mapped automatically to underlying processor cores

Page 23: Java Streams: Overview & Benefits · 3 Learning Objectives in this Part of the Lesson •Understand the structure & functionality of Java streams, e.g., •Fundamentals of streams

23

End of Java Streams: Overview & Benefits