Overview of Java Streams
Douglas C. Schmidt
[Link]@[Link]
[Link]/~schmidt
Professor of Computer Science
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
Learning Objectives in this Part of the Lesson
• Understand Java streams structure &
Stream source
functionality
Input x
Aggregate operation (behavior f)
Output f(x)
Aggregate operation (behavior g)
Output g(f(x))
Aggregate operation (behavior h)
2
Learning Objectives in this Part of the Lesson
• Understand Java streams structure &
Stream source
functionality, e.g.
Input x
• Fundamentals of streams
Aggregate operation (behavior f)
Output f(x)
Aggregate operation (behavior g)
Output g(f(x))
Aggregate operation (behavior h)
3
Learning Objectives in this Part of the Lesson
• Understand Java streams structure &
Stream source
functionality, e.g.
Input x
• Fundamentals of streams
• & the evolution of streams Aggregate operation (behavior f)
Output f(x)
Aggregate operation (behavior g)
Output g(f(x))
Aggregate operation (behavior h)
4
Overview of
Java Streams
5
Overview of Java Streams
• Java streams are a framework first
introduced into the Java class library
in Java 8
See [Link]/javase/tutorial/collections/streams
6
Overview of Java Streams
• A stream is a pipeline of aggregate operations that process a sequence of
elements (aka, “values” or “data”) Input x
Aggregate operation (behavior f)
Output f(x)
Aggregate operation (behavior g)
Output g(f(x))
Aggregate operation (behavior h)
See [Link]/javase/tutorial/collections/streams
7
Overview of Java Streams
• A stream is a pipeline of aggregate operations that process a sequence of
elements (aka, “values” or “data”) Input x
Aggregate operation (behavior f)
Output f(x)
Aggregate operation (behavior g)
Output g(f(x))
An aggregate operation is a higher- Aggregate operation (behavior h)
order function that applies a “behavior”
param to every element in a stream.
See [Link]/wiki/Higher-order_function
8
Overview of Java Streams
• A stream is a pipeline of aggregate operations that process a sequence of
elements (aka, “values” or “data”) Input x
Aggregate operation (behavior f)
Output f(x)
Aggregate operation (behavior g)
Output g(f(x))
Aggregate operation (behavior h)
Behavior parameterization simplifies
coping with changing requirements.
See [Link]/articles/java-8-behavior-parameterization
9
Overview of Java Streams
• A stream is a pipeline of aggregate operations that process a sequence of
elements (aka, “values” or “data”) Input x
Aggregate operation (behavior f)
Output f(x)
Aggregate operation (behavior g)
Output g(f(x))
Aggregate operation (behavior h)
A stream is conceptually unbounded, though it’s often bounded by practical constraints.
10
Overview of Java Streams
• A Java stream is an implementation of the POSA1 Pipes & Filters pattern
Divide an app’s tasks into multiple self-contained data
processing steps & connect these steps via intermediate
data buffers to form a data processing pipeline
See [Link]/plop/2011/papers/[Link]
11
Overview of Java Streams
• We use this stream as a case study example throughout this introduction
Stream Input x
.of("Ophelia","horatio",
"laertes","Gertrude", Aggregate operation (behavior f)
"Hamlet","fortinbras", ...)
.filter(s -> toLowerCase Output f(x)
([Link](0)) == 'h')
.map(this::capitalize) Aggregate operation (behavior g)
.sorted()
.forEach([Link]::println); Output g(f(x))
Aggregate operation (behavior h)
Print each character in Hamlet that starts with ‘H’
or ‘h’ in consistently capitalized & sorted order.
See [Link]/douglascraigschmidt/LiveLessons/tree/master/Java8/ex12
12
The Evolution of
Java Streams
13
The Evolution of Java Streams
• Java streams have evolved a bit over time
14
The Evolution of Java Streams
• Java streams have evolved a bit over time, e.g.
• Later versions of Java added some
new operations
See [Link]/java-9-stream-api
15 & [Link]/java/teeing-collector
The Evolution of Java Streams
• Java streams have evolved a bit over time, e.g.
• Later versions of Java added some
new operations
• Java 9 also added a new API that
implements the reactive streams
specification
See [Link]
16
The Evolution of Java Streams
• Java streams have evolved a bit over time, e.g.
• Later versions of Java added some
new operations
• Java 9 also added a new API that
implements the reactive streams
specification
• Reactive streams frameworks
are covered later in this course
See upcoming lessons on17RxJava & Project Reactor
End of Overview
of Java Streams
18