45
Overview of Java 8 Streams (Part 2) Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

Overview of Java 8 Streams (Part 2)

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Professor of Computer ScienceInstitute for Software Integrated Systems

Vanderbilt University Nashville, Tennessee, USA

Page 2: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

2

Learning Objectives in this Part of the Lesson• Understand the structure & functionality of Java 8 streams, e.g.,

• Fundamentals of streams• Common stream aggregate

operations Intermediate operation (Behavior f)

Input x

Output f(x)

Output g(f(x))

Intermediate operation (Behavior g)

Terminal operation (Behavior h)

Both intermediate & terminal operations

Page 3: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

3

Learning Objectives in this Part of the Lesson• Understand the structure & functionality of Java 8 streams, e.g.,

• Fundamentals of streams• Common stream aggregate

operations• These operations apply to both

sequential & parallel streams

Input x

Output f(x)

Output g(f(x))

Intermediate operation (Behavior f)

Intermediate operation (Behavior g)

Terminal operation (Behavior h)

Page 4: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

4

Learning Objectives in this Part of the Lesson• Understand the structure & functionality of Java 8 streams, e.g.,

• Fundamentals of streams• Common stream aggregate

operations• These operations apply to both

sequential & parallel streams

Being a good streams programmer makes you a better parallel streams programmer

Input x

Output f(x)

Output g(f(x))

Intermediate operation (Behavior f)

Intermediate operation (Behavior g)

Terminal operation (Behavior h)

Page 5: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

5

Learning Objectives in this Part of the Lesson• Understand the structure & functionality of Java 8 streams, e.g.,

• Fundamentals of streams• Common stream aggregate

operations• These operations apply to both

sequential & parallel streams

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

We’ll use a simple sequential stream example to explain common

Java 8 aggregate operations

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

collect(toList())

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

Input String to Search

Page 6: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

6

Overview of SimpleSearchStream Example

Page 7: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

7

• This example finds words in an input stringOverview of SimpleSearchStream Example

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

We’ll use this example to explain Java 8 aggregate operations

throughout this part of the lesson

Input String to Search

Let's start at the very beginning…

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

collect(toList())

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Page 8: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

8

• This example finds words in an input stringOverview of SimpleSearchStream Example

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

Starting SimpleSearchStreamWord "Re" matched at index [131|141|151|202|212|222|

979|1025|1219|1259|1278|1300|1351|1370|1835|1875|1899|1939|2266|2295]

Word "Ti" matched at index [237|994|1272|1294|1364|1850|1860|1912|1915|1952|1955|2299]

Word "La" matched at index [234|417|658|886|991|1207|1247|1269|1291|1339|1361|1742|1847|1863|1909|1949|2161|2254|2276|2283]...

Ending SimpleSearchSTream

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

collect(toList())

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Input String to Search

Let's start at the very beginning…

Page 9: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

9

• This example finds words in an input stringOverview of SimpleSearchStream Example

List<String> …

Input a list of words to find 45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Page 10: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

10

• This example finds words in an input stringOverview of SimpleSearchStream Example

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Convert collection to a (sequential) stream

Page 11: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

11

Output a stream of words to find

• This example finds words in an input stringOverview of SimpleSearchStream Example

…Stream<String>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Page 12: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

12

Input a stream of words to find

• This example finds words in an input stringOverview of SimpleSearchStream Example

map(this::searchForWord)

Stream<String>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Page 13: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

13

• This example finds words in an input stringOverview of SimpleSearchStream Example

map(this::searchForWord)

Stream<String>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Search for the word in the input string

Page 14: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

14

Output a stream of search results

• This example finds words in an input stringOverview of SimpleSearchStream Example

map(this::searchForWord)Stream<SearchResults>

Stream<String>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

SearchResults stores # of times a word appeared in the input string

Page 15: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

15

Input a stream of search results

• This example finds words in an input stringOverview of SimpleSearchStream Example

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

Stream<SearchResults>

Stream<String>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Page 16: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

16

• This example finds words in an input stringOverview of SimpleSearchStream Example

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

Stream<SearchResults>

Stream<String>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Remove empty search results from the stream

Page 17: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

17

Output a stream of non-empty search results

• This example finds words in an input stringOverview of SimpleSearchStream Example

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Page 18: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

18

Input a stream of non-empty search results

• This example finds words in an input stringOverview of SimpleSearchStream Example

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

collect(toList())

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Page 19: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

19

• This example finds words in an input stringOverview of SimpleSearchStream Example

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

collect(toList())

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Trigger intermediate operation processing

Page 20: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

20

• This example finds words in an input stringOverview of SimpleSearchStream Example

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

collect(toList())

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Return a list of search results

Page 21: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

21

• The “physical” processing of a stream differs from the “logical” model• i.e., each element is “pulled” from the source

through each aggregate operation

Overview of SimpleSearchStream Example

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

collect(toList())

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

See www.ibm.com/developerworks/library/j-java-streams-3-brian-goetz

Page 22: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

22

Overview of Common Stream Aggregate

Operations

Page 23: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

23

Input x

Output f(x)

Output g(f(x))

Overview of Common Stream Aggregate Operations• An aggregate operation is a higher-order function that applies a “behavior”

on elements in a stream

Aggregate operation (Behavior f)

Aggregate operation (Behavior g)

Aggregate operation (Behavior h)

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

Output h(g(f(x)))

Page 24: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

24

Input x

Output f(x)

Output g(f(x))

Overview of Common Stream Aggregate Operations• An aggregate operation is a higher-order function that applies a “behavior”

on elements in a stream

Aggregate operation (Behavior f)

Aggregate operation (Behavior g)

Aggregate operation (Behavior h)

The behavior can be a lambda or method reference to a function,

predicate, consumer, supplier, etc.

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

Output h(g(f(x)))

Page 25: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

25

Input x

Output f(x)

Output g(f(x))

Overview of Common Stream Aggregate Operations• Aggregate operations focus on “what” (declarative), not “how” (imperative)

Aggregate operation (Behavior f)

Aggregate operation (Behavior g)

Aggregate operation (Behavior h)

See blog.jooq.org/2015/09/17/comparing-imperative-and-functional-algorithms-in-java-8

What

How

Output h(g(f(x)))

Page 26: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

26

Overview of Common Stream Aggregate Operations

Input x

Output f(x)

Output g(f(x))

Intermediate operation (Behavior f)

Intermediate operation (Behavior g)

Terminal operation (Behavior h)

• There are two types of aggregate operations

Page 27: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

27

• There are two types of aggregate operations• Intermediate operations

• Process elements in their input stream & yield an output stream• e.g., filter(), map(), flatMap(), etc.

Overview of Common Stream Aggregate Operations

Input x

Output f(x)

Output g(f(x))

Intermediate operation (Behavior f)

Intermediate operation (Behavior g)

Terminal operation (Behavior h)

Page 28: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

28

• There are two types of aggregate operations• Intermediate operations • Terminal operations

• Trigger intermediate operations &produce a non-stream result• e.g., forEach(), reduce(), collect(),

etc.

Overview of Common Stream Aggregate Operations

Input x

Output f(x)

Output g(f(x))

Intermediate operation (Behavior f)

Intermediate operation (Behavior g)

Terminal operation (Behavior h)

Page 29: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

29

• Overview of the map() intermediate operation Overview of Common Stream Aggregate Operations

Stream map(Function<…> mapper)

Stream filter(Predicate<…> pred)

R collect(Collector<…> collector)

See docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#map

Input x

Output f(x)

Output g(f(x))

Applies the mapper function to every element of the input stream & returns

an output stream consisting of the results

Page 30: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

30

• Overview of the map() intermediate operation Overview of Common Stream Aggregate Operations

Stream map(Function<…> mapper)

Stream filter(Predicate<…> pred)

R collect(Collector<…> collector)

The # of output stream elements matches the # of input stream elements

Input x

Output f(x)

Output g(f(x))

Applies the mapper function to every element of the input stream & returns

an output stream consisting of the results

Page 31: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

31

• Overview of the map() intermediate operation Overview of Common Stream Aggregate Operations

For each word to find determine the indices (if any) where the word matches the input string

map(this::searchForWord)Stream<SearchResults>

Stream<String>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Page 32: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

32

• Overview of the map() intermediate operation Overview of Common Stream Aggregate Operations

map(this::searchForWord)Stream<SearchResults>

Stream<String>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

map() may transform the type of elements it processes

Page 33: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

33

• Overview of the map() intermediate operation Overview of Common Stream Aggregate Operations

map(this::searchForWord)

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()List<SearchResults> results = wordsToFind.stream().map(this::searchForWord).filter(not

(SearchResults::isEmpty)).collect(toList());

Page 34: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

34

• Overview of the filter() intermediate operation Overview of Common Stream Aggregate Operations

See docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#filter

Tests the given predicate against each element of the input stream & returns an output stream consisting only of

the elements that match the predicate

Stream map(Function<…> mapper)

Stream filter(Predicate<…> pred)

R collect(Collector<…> collector)

Input x

Output f(x)

Output g(f(x))

Page 35: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

35

• Overview of the filter() intermediate operation Overview of Common Stream Aggregate Operations

Tests the given predicate against each element of the input stream & returns an output stream consisting only of

the elements that match the predicate

Stream map(Function<…> mapper)

Stream filter(Predicate<…> pred)

R collect(Collector<…> collector)

The # of output stream elements may be less than the # of input stream elements

Input x

Output f(x)

Output g(f(x))

Page 36: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

36

• Overview of the filter() intermediate operation Overview of Common Stream Aggregate Operations

Filter out empty SearchResults

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

The filter() aggregate operation can’t change the type of elements it processes

Page 37: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

37

• Overview of the filter() intermediate operation Overview of Common Stream Aggregate Operations

Filter out empty SearchResults

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()List<SearchResults> results = wordsToFind.stream().map(this::searchForWord).filter(not

(SearchResults::isEmpty)).collect(toList());

Page 38: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

38

• Intermediate operations are “lazy” & don’t run until a terminal operator is reached

Overview of Common Stream Aggregate Operations

Stream map(Function<…> mapper)

Stream filter(Predicate<…> pred)

R collect(Collector<…> collector)

See www.logicbig.com/tutorials/core-java-tutorial/java-util-stream/lazy-evaluation

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

Page 39: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

39

• A terminal operation triggers intermediate operation processingOverview of Common Stream Aggregate Operations

Stream map(Function<…> mapper)

Stream filter(Predicate<…> pred)

R collect(Collector<…> collector)

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

Page 40: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

40

• Overview of the collect() terminal operation Overview of Common Stream Aggregate Operations

See docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#collect

Stream map(Function<…> mapper)

Stream filter(Predicate<…> pred)

R collect(Collector<…> collector)

This terminal operation uses a collector to perform a reduction on the elements of its input stream & returns the results of the reduction

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

Page 41: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

41

collect(toList())

• Overview of the collect() terminal operation Overview of Common Stream Aggregate Operations

Triggers intermediate operation processing

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

Stream<SearchResults>

Stream<String>

Stream<SearchResults>

List<SearchResults>

List<String> …

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Page 42: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

42

List<SearchResults> results = wordsToFind.stream().map(this::searchForWord).filter(not

(SearchResults::isEmpty)).collect(toList());

collect(toList())

Create a list of SearchResults

• Overview of the collect() terminal operation Overview of Common Stream Aggregate Operations

map(this::searchForWord)

filter(not(SearchResults::isEmpty))

45,000+ phrases"do", "re", "mi", "fa","so", "la", "ti", "do"

Search Words

stream()

Page 43: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

43

• An aggregate operation may process all elements in a stream, e.g.• map() processes all of the elements

in its input stream

Overview of Common Stream Aggregate Operations

Stream map(Function<…> mapper)

Stream limit(long maxSize)

Optional findFirst()

Input x

Output f(x)

Output g(f(x))

Page 44: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

44

• An aggregate operation may process all elements in a stream, e.g.• map() processes all of the elements

in its input stream• limit() & findFirst() are “short-circuit”

operations that halt further processingafter their condition is reached

Overview of Common Stream Aggregate Operations

Stream map(Function<…> mapper)

Stream limit(long maxSize)

Optional findFirst()

Input x

Output f(x)

Output g(f(x))

See www.logicbig.com/tutorials/core-java-tutorial/java-util-stream/short-circuiting

Page 45: Overview of Java 8 Streams (Part 2) - Vanderbilt …schmidt/cs891f/2018-PDFs/06-Java...2 Learning Objectives in this Part of the Lesson • Understand the structure & functionality

45

End of Overview of Java 8 Streams (Part 2)