forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplyFunctional.java
More file actions
49 lines (46 loc) · 1.18 KB
/
ApplyFunctional.java
File metadata and controls
49 lines (46 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// generics/ApplyFunctional.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.*;
import java.util.stream.*;
import java.util.function.*;
import onjava.*;
public class ApplyFunctional {
public static void main(String[] args) {
Stream.of(
Stream.generate(Shape::new).limit(2),
Stream.generate(Square::new).limit(2))
.flatMap(c -> c) // flatten into one stream
.peek(Shape::rotate)
.forEach(s -> s.resize(7));
new FilledList<>(Shape::new, 2)
.forEach(Shape::rotate);
new FilledList<>(Square::new, 2)
.forEach(Shape::rotate);
SimpleQueue<Shape> shapeQ = Suppliers.fill(
new SimpleQueue<>(), SimpleQueue::add,
Shape::new, 2);
Suppliers.fill(shapeQ, SimpleQueue::add,
Square::new, 2);
shapeQ.forEach(Shape::rotate);
}
}
/* Output:
Shape 0 rotate
Shape 0 resize 7
Shape 1 rotate
Shape 1 resize 7
Square 2 rotate
Square 2 resize 7
Square 3 rotate
Square 3 resize 7
Shape 4 rotate
Shape 5 rotate
Square 6 rotate
Square 7 rotate
Shape 8 rotate
Shape 9 rotate
Square 10 rotate
Square 11 rotate
*/