1.3.1 Java Streams Overview
1.3.1 Java Streams Overview
Douglas C. Schmidt
[email protected]
www.dre.vanderbilt.edu/~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
Output f(x)
Output g(f(x))
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)
Output g(f(x))
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)
Output g(f(x))
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 docs.oracle.com/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
Output f(x)
Output g(f(x))
See docs.oracle.com/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
Output f(x)
Output g(f(x))
See en.wikipedia.org/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
Output f(x)
Output g(f(x))
See blog.indrek.io/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
Output f(x)
Output g(f(x))
10
Overview of Java Streams
• A Java stream is an implementation of the POSA1 Pipes & Filters pattern
See hillside.net/plop/2011/papers/B-10-Hanmer.pdf
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)
(s.charAt(0)) == 'h')
.map(this::capitalize) Aggregate operation (behavior g)
.sorted()
.forEach(System.out::println); Output g(f(x))
See github.com/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 www.baeldung.com/java-9-stream-api
15 & blog.codefx.org/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 www.reactive-streams.org
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
18