20
Java Streams: Other 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: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

Java Streams: Other 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: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

2

• Understand common factory methods used to create streams

• Recognize other factory methods used to create streams

Learning Objectives in this Part of the Lesson

Page 3: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

3

Other Factory Methods for Creating Streams

Page 4: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

4

• There are several other ways to obtain a stream

Other Factory Methods for Creating Streams

Page 5: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

5

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

• I/O stream classes, e.g.

• BufferedReader.lines() obtains lines of a file

void printFileLines

(String filename){

try (BufferedReader reader =

Files.newBufferedReader

(Paths.get(filename)) {

reader

.lines()

.forEach

(System.out::println);

} catch (IOException ex) {...}

}Create a buffered reader from a given filename.

Other Factory Methods for Creating Streams

See docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html

Page 6: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

6

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

• I/O stream classes, e.g.

• BufferedReader.lines() obtains lines of a file

void printFileLines

(String filename){

try (BufferedReader reader =

Files.newBufferedReader

(Paths.get(filename)) {

reader

.lines()

.forEach

(System.out::println);

} catch (IOException ex) {...}

}

The buffered reader will beclosed automatically after thetry-with-resources block exits.

Other Factory Methods for Creating Streams

See docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Page 7: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

7

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

• I/O stream classes, e.g.

• BufferedReader.lines() obtains lines of a file

void printFileLines

(String filename){

try (BufferedReader reader =

Files.newBufferedReader

(Paths.get(filename)) {

reader

.lines()

.forEach

(System.out::println);

} catch (IOException ex) {...}

}Create stream containing all of the lines in a file.

Other Factory Methods for Creating Streams

See docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#lines

Page 8: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

8

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

• I/O stream classes, e.g.

• BufferedReader.lines() obtains lines of a file

void printFileLines

(String filename){

try (BufferedReader reader =

Files.newBufferedReader

(Paths.get(filename)) {

reader

.lines()

.forEach

(System.out::println);

} catch (IOException ex) {...}

}Print each of the lines in the stream.

Other Factory Methods for Creating Streams

Page 9: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

9

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

• I/O stream classes, e.g.

• BufferedReader.lines() obtains lines of a file

• Streams of file paths & lines can be obtained from Files methods

void printFileLines

(String filename){

try(Stream<String> stream =

Files.lines

(Paths.get(fileName))) {

stream.forEach

(System.out::println);

} catch (IOException ex) {...}

}

Other Factory Methods for Creating Streams

See docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#lines

Create stream containing all of the lines in a file

Page 10: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

10

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

• I/O stream classes, e.g.

• BufferedReader.lines() obtains lines of a file

• Streams of file paths & lines can be obtained from Files methods

void printFileLines

(String filename){

try(Stream<String> stream =

Files.lines

(Paths.get(fileName))) {

stream.forEach

(System.out::println);

} catch (IOException ex) {...}

}

Other Factory Methods for Creating Streams

Print each of the lines in the stream

Page 11: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

11

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

• A stream of random #’s can be obtained from Random.ints()

new Random()

.ints(0,100)

.limit(50)

.forEach(System.out::println);

Other Factory Methods for Creating Streams

Page 12: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

12

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

• A stream of random #’s can be obtained from Random.ints()

new Random()

.ints(0,100)

.limit(50)

.forEach(System.out::println);

Generate an “unbounded” stream of random #’s ranging between 0 & 100

Other Factory Methods for Creating Streams

See docs.oracle.com/javase/8/docs/api/java/util/Random.html#ints

Page 13: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

13

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

• A stream of random #’s can be obtained from Random.ints()

new Random()

.ints(0,100)

.limit(50)

.forEach(System.out::println);

Other Factory Methods for Creating Streams

Limit the size of the stream to 50 elements

Page 14: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

14

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

• A stream of random #’s can be obtained from Random.ints()

new Random()

.ints(0,100)

.limit(50)

.forEach(System.out::println);

Other Factory Methods for Creating Streams

Print each random # in the stream

Page 15: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

15

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

• StreamSupport.stream() factory method

SearchResults searchForPhrase

(String phrase, CharSequence input,

String title, boolean parallel) {

return new SearchResults

(..., phrase, ..., StreamSupport

.stream(new PhraseMatchSpliterator

(input, phrase),

parallel)

.collect(toList()));

}

Other Factory Methods for Creating Streams

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

Create a stream that contains all the phrases that match in an input string

Page 16: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

16

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

• Other JDK stream-bearing methods

Stream<String> getInputData

(String filename,

String splitter){

return Pattern

.compile(splitter)

.splitAsStream

(new String

(Files.readAllBytes

(Paths.get(filename)

.toURI())));

}

Splits a file into a stream of strings

Other Factory Methods for Creating Streams

See docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#splitAsStream

Page 17: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

17

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

• Other JDK stream-bearing methods

List<TreeMap<Long, String>>

listOfTreeMaps =

Stream.generate

(TreeMap<Long, String>::new)

.limit(100)

.collect(toList());

Generate an “infinite” stream of TreeMaps

Other Factory Methods for Creating Streams

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

Page 18: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

18

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

• Other JDK stream-bearing methods

List<TreeMap<Long, String>>

listOfTreeMaps =

Stream.generate

(TreeMap<Long, String>::new)

.limit(100)

.collect(toList());

Other Factory Methods for Creating Streams

Limit the stream to 100 elements

Page 19: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

19

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

• Other JDK stream-bearing methods

List<TreeMap<Long, String>>

listOfTreeMaps =

Stream.generate

(TreeMap<Long, String>::new)

.limit(100)

.collect(toList());

Other Factory Methods for Creating Streams

Create a list of 100 TreeMaps

Page 20: Java Streams: Other Factory Methodsschmidt/cs891f/2019-PDFs/X.3.2... · 2019-09-20 · 2 •Understand common factory methods used to create streams •Recognize other factory methods

20

End of Java Streams: Other Factory Methods