java streams: overview & benefits · 3 learning objectives in this part of the lesson...
Post on 20-Aug-2020
7 Views
Preview:
TRANSCRIPT
Java Streams: Overview & Benefits
Douglas C. Schmidtd.schmidt@vanderbilt.edu
www.dre.vanderbilt.edu/~schmidt
Professor of Computer Science
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
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))
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))
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))
5
Overview of Java 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
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
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
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.
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
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
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.
13
Benefits of Java 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()
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
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
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
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()
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.
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
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
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
23
End of Java Streams: Overview & Benefits
top related