24
Java Streams: Common Factory Methods Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

Java Streams: Common Factory Methods

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Professor of Computer Science

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

2

• Understand common factory methods used to create streams

Learning Objectives in this Part of the Lesson

Page 3: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

3

Common Factory Methods for Creating Streams

Page 4: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

4See docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

Common Factory Methods for Creating Streams• There are several common ways to obtain a stream

Page 5: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

5

• There are several common ways to obtain a stream, e.g.

• From a Java collection

or

Common Factory Methods for Creating Streams

List<String> wordsToFind =

Arrays.asList("do", "re", "me", ...);

List<SearchResults> results =

wordsToFind.stream()

...

List<SearchResults> results =

wordsToFind.parallelStream()

...

Page 6: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

6

• There are several common ways to obtain a stream, e.g.

• From a Java collection

Common Factory Methods for Creating Streams

List<SearchResults> results =

wordsToFind.parallelStream()

...

or

List<String> wordsToFind =

Arrays.asList("do", "re", "me", ...);

List<SearchResults> results =

wordsToFind.stream()

...

See docs.oracle.com/javase/tutorial/collections/streams

Page 7: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

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

• There are several common ways to obtain a stream, e.g.

• From a Java collection

Common Factory Methods for Creating Streams

List<SearchResults> results =

wordsToFind.parallelStream()

...

or

List<String> wordsToFind =

Arrays.asList("do", "re", "me", ...);

List<SearchResults> results =

wordsToFind.stream()

...

We use this approach in the SimpleSearchStream program

Page 8: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

8

• There are several common ways to obtain a stream, e.g.

• From a Java collection

See docs.oracle.com/javase/tutorial/collections/streams/parallelism.html

Common Factory Methods for Creating Streams

or

List<String> wordsToFind =

Arrays.asList("do", "re", "me", ...);

List<SearchResults> results =

wordsToFind.stream()

...

List<SearchResults> results =

wordsToFind.parallelStream()

...

Page 9: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

9

• There are several common ways to obtain a stream, e.g.

• From a Java collection

See docs.oracle.com/javase/8/docs/api/java/util/stream/BaseStream.html#parallel

Common Factory Methods for Creating Streams

A call to parallel() can appear anywhere in a

stream & will have same effect as parallelStream()

or

List<String> wordsToFind =

Arrays.asList("do", "re", "me", ...);

List<SearchResults> results =

wordsToFind.stream()

...

List<SearchResults> results =

wordsToFind.stream()

...

.parallel()

Page 10: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

10

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an arrayString[] a = {

"a", "b", "c", "d", "e"

};

Stream<String> stream = Arrays.stream(a);

stream.forEach(s ->

System.out.println(s));

or

stream.forEach(System.out::println);

Common Factory Methods for Creating Streams

Page 11: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

11

String[] a = {

"a", "b", "c", "d", "e"

};

Stream<String> stream = Arrays.stream(a);

stream.forEach(s ->

System.out.println(s));

or

stream.forEach(System.out::println);

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

Create stream containing all elements in an array

Common Factory Methods for Creating Streams

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

Page 12: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

12

String[] a = {

"a", "b", "c", "d", "e"

};

Stream<String> stream = Arrays.stream(a);

stream.forEach(s ->

System.out.println(s));

or

stream.forEach(System.out::println);

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

Print all elements in the stream

Common Factory Methods for Creating Streams

Page 13: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

13

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

String[] a = {

"a", "b", "c", "d", "e"

};

Stream<String> stream = Stream.of(a);

stream.forEach(s ->

System.out.println(s));

or

stream.forEach(System.out::println);

Common Factory Methods for Creating Streams

Page 14: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

14

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

String[] a = {

"a", "b", "c", "d", "e"

};

Stream<String> stream = Stream.of(a);

stream.forEach(s ->

System.out.println(s));

or

stream.forEach(System.out::println);

Common Factory Methods for Creating Streams

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

Create stream containing all elements in an array

Page 15: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

15

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

String[] a = {

"a", "b", "c", "d", "e"

};

Stream<String> stream = Stream.of(a);

stream.forEach(s ->

System.out.println(s));

or

stream.forEach(System.out::println);

Common Factory Methods for Creating Streams

Print all elements in the stream

Page 16: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

16

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

Stream.iterate(new BigInteger[]{BigInteger.ONE,

BigInteger.ONE},

f -> new BigInteger[]{f[1],

f[0].add(f[1])})

.map(f -> f[0])

.limit(100)

.forEach(System.out::println);

Common Factory Methods for Creating Streams

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

Page 17: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

17

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

Stream.iterate(new BigInteger[]{BigInteger.ONE,

BigInteger.ONE},

f -> new BigInteger[]{f[1],

f[0].add(f[1])})

.map(f -> f[0])

.limit(100)

.forEach(System.out::println);

Generate & print the first 100 Fibonacci #’s

Common Factory Methods for Creating Streams

Page 18: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

18

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

Stream.iterate(new BigInteger[]{BigInteger.ONE,

BigInteger.ONE},

f -> new BigInteger[]{f[1],

f[0].add(f[1])})

.map(f -> f[0])

.limit(100)

.forEach(System.out::println);

Create the “seed,” which defines the initial element in the stream

Common Factory Methods for Creating Streams

Page 19: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

19

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

Stream.iterate(new BigInteger[]{BigInteger.ONE,

BigInteger.ONE},

f -> new BigInteger[]{f[1],

f[0].add(f[1])})

.map(f -> f[0])

.limit(100)

.forEach(System.out::println);

A lambda function applied to the previous element to

produce a new element

Common Factory Methods for Creating Streams

Page 20: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

20

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

Stream.iterate(new BigInteger[]{BigInteger.ONE,

BigInteger.ONE},

f -> new BigInteger[]{f[1],

f[0].add(f[1])})

.map(f -> f[0])

.limit(100)

.forEach(System.out::println);

Convert the array to its first element

Common Factory Methods for Creating Streams

Page 21: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

21

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

Stream.iterate(new BigInteger[]{BigInteger.ONE,

BigInteger.ONE},

f -> new BigInteger[]{f[1],

f[0].add(f[1])})

.map(f -> f[0])

.limit(100)

.forEach(System.out::println);

Common Factory Methods for Creating Streams

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

Limit the stream to 100 elements

Page 22: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

22

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

Stream.iterate(new BigInteger[]{BigInteger.ONE,

BigInteger.ONE},

f -> new BigInteger[]{f[1],

f[0].add(f[1])})

.map(f -> f[0])

.limit(100)

.forEach(System.out::println); Print the Fibonacci #’s

Common Factory Methods for Creating Streams

Page 23: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

23

• There are several common ways to obtain a stream, e.g.

• From a Java collection

• From an array

• From a static factory method

Stream.iterate(new BigInteger[]{BigInteger.ONE,

BigInteger.ONE},

f -> new BigInteger[]{f[1],

f[0].add(f[1])})

.parallel()

.map(f -> f[0])

.limit(100)

.forEach(System.out::println);

Common Factory Methods for Creating Streams

Avoid using iterate() in a parallel stream!

Page 24: Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a stream, e.g. •From a Java collection •From an array •From a static factory method

24

End of Java Streams: Common Factory Methods