1.3.2 Java Streams Common Operations
1.3.2 Java Streams Common Operations
Common Operations
Douglas C. Schmidt
d.schmidt@vanderbilt.edu
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 &
functionality, e.g.
Stream source
• Fundamentals of streams
Input x
• Three streams phases
• Operations that create a stream Aggregate operation (behavior f)
Output f(x)
Output g(f(x))
2
Learning Objectives in this Part of the Lesson
• Understand Java streams structure &
functionality, e.g.
Stream source
• Fundamentals of streams
Input x
• Three streams phases
• Operations that create a stream Aggregate operation (behavior f)
• Aggregate operations in a stream
Output f(x)
Output g(f(x))
3
Operations that Create
a Java Stream
4
Operations that Create a Java Stream
• A factory method creates a stream from Stream source
some source Input x
Stream
.of("horatio", Aggregate operation (behavior f)
"laertes", Output f(x)
"Hamlet", ...)
...
Aggregate operation (behavior g)
Output g(f(x))
See en.wikipedia.org/wiki/Factory_method_pattern
5
Operations that Create a Java Stream
• A factory method creates a stream from Stream source
some source Input x
Stream
.of("horatio", Aggregate operation (behavior f)
"laertes", Output f(x)
"Hamlet", ...) ...
Aggregate operation (behavior g)
Array
<String> “horatio” “laertes” “Hamlet” … Output g(f(x))
Stream …
“horatio” “laertes” “Hamlet”
<String>
Aggregate operation (behavior h)
See docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#of
6
Operations that Create a Java Stream
• Many factory methods create streams
collection.stream() Arrays.stream(array)
collection.parallelStream() Arrays.stream(array, start, end)
Pattern.compile(…).splitAsStream() Files.lines(file_path)
Stream.of(value1,… ,valueN) "string".chars()
StreamSupport Stream.iterate(init_value,
.stream(iterable.spliterator(), generate_expression)
false) Stream.builder().add(...).build()
... Stream.generate(supplier)
Files.list(file_path)
Files.find(file_path, max_depth,
matcher)
...
7
Operations that Create a Java Stream
• Many factory methods create streams
collection.stream() Arrays.stream(array)
collection.parallelStream() Arrays.stream(array, start, end)
Pattern.compile(…).splitAsStream() Files.lines(file_path)
Stream.of(value1,… ,valueN) "string".chars()
StreamSupport Stream.iterate(init_value,
.stream(iterable.spliterator(), generate_expression)
false) Stream.builder().add(...).build()
... Stream.generate(supplier)
Files.list(file_path)
Files.find(file_path, max_depth,
These are key factory methods matcher)
that we focus on in this course. ...
9
Java Streams Aggregate Operations
• An aggregate operation performs a behavior Stream source
on elements in a stream
Input x
Aggregate operation (behavior f)
See blog.indrek.io/articles/java-8-behavior-parameterization
10
Java Streams Aggregate Operations
• An aggregate operation performs a behavior Stream source
on elements in a stream
Input x
Stream
.of("horatio", Aggregate operation (behavior f)
"laertes",
"Hamlet", ...)
.filter(s -> toLowerCase
(s.charAt(0)) == 'h')
.map(this::capitalize)
.sorted() Stream
.forEach(System.out::println); <String> “horatio” “Hamlet”
Stream
<String> “Horatio”
Method reference “Hamlet”
See github.com/douglascraigschmidt/LiveLessons/tree/master/Java8/ex12
11
Java Streams Aggregate Operations
• An aggregate operation performs a behavior
on elements in a stream
• Some aggregate operations perform
behaviors on all elements in a stream
12
Java Streams Aggregate Operations
• An aggregate operation performs a behavior
on elements in a stream
• Some aggregate operations perform
behaviors on all elements in a stream
• Other aggregate operations perform
behaviors on some elements in a stream
13
Java Streams Aggregate Operations
• Aggregate operations can be composed Stream source
to form a pipeline of processing phases
Input x
Aggregate operation (behavior f)
Output f(x)
See en.wikipedia.org/wiki/Pipeline_(software)
14
Java Streams Aggregate Operations
• Aggregate operations can be composed Stream source
to form a pipeline of processing phases
Input x
Aggregate operation (behavior f)
Output f(x)
The output of one aggregate operation can be input into the next one in the stream.
15
Java Streams Aggregate Operations
• Aggregate operations can be composed Stream source
to form a pipeline of processing phases
Input x
Stream
.of("horatio", Aggregate operation (behavior f)
"laertes", Output f(x)
"Hamlet", ...)
.filter(s -> toLowerCase Aggregate operation (behavior g)
(s.charAt(0)) == 'h')
.map(this::capitalize) Output g(f(x))
.sorted()
.forEach(System.out::println); Aggregate operation (behavior h)
See en.wikipedia.org/wiki/Fluent_interface
16
Java Streams Aggregate Operations
• Aggregate operations can be composed Stream source
to form a pipeline of processing phases
Input x
Stream
.of("horatio", Aggregate operation (behavior f)
"laertes", Output f(x)
"Hamlet", ...)
.filter(s -> toLowerCase Aggregate operation (behavior g)
(s.charAt(0)) == 'h')
.map(this::capitalize) Output g(f(x))
.sorted()
.forEach(System.out::println); Aggregate operation (behavior h)
See docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#sorted
20
Java Streams Aggregate Operations
• Aggregate operations can be composed Stream source
to form a pipeline of processing phases
Input x
Stream
.of("horatio", Aggregate operation (behavior f)
"laertes", Output f(x)
"Hamlet", ...)
.filter(s -> toLowerCase Aggregate operation (behavior g)
(s.charAt(0)) == 'h')
.map(this::capitalize) Output g(f(x))
.sorted()
.forEach(System.out::println); Aggregate operation (behavior h)
24