Effective Java, Chapter 3: Methods Common to All Objects Paul Ammann.
Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a...
Transcript of Java Streams: Common Factory Methods€¦ · 14 •There are several common ways to obtain a...
![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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a6c10a5a7c110a7195ab3/html5/thumbnails/24.jpg)
24
End of Java Streams: Common Factory Methods