java streams: other factory methodsschmidt/cs891f/2019-pdfs/x.3.2... · 2019-09-20 · 2...
TRANSCRIPT
![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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022043010/5fa1cf082912043998551f05/html5/thumbnails/20.jpg)
20
End of Java Streams: Other Factory Methods