-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStreamsExample.java
106 lines (83 loc) · 2.93 KB
/
StreamsExample.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package java8;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
public class StreamsExample {
public static void main(final String[] args) {
List<Author> authors = Library.getAuthors();
banner("Authors information");
// TODO With functional interfaces declared
Consumer<Author> printer = System.out::println;
authors
.forEach(printer);
// TODO With functional interfaces used directly
authors
.forEach(System.out::println);
banner("Active authors");
// TODO With functional interfaces declared
// TODO With functional interfaces used directly
banner("Active books for all authors");
// TODO With functional interfaces declared
// TODO With functional interfaces used directly
banner("Average price for all books in the library");
// TODO With functional interfaces declared
// TODO With functional interfaces used directly
banner("Active authors that have at least one published book");
// TODO With functional interfaces declared
// TODO With functional interfaces used directly
}
private static void banner(final String m) {
System.out.println("#### " + m + " ####");
}
}
class Library {
public static List<Author> getAuthors() {
return Arrays.asList(
new Author("Author A", true, Arrays.asList(
new Book("A1", 100, true),
new Book("A2", 200, true),
new Book("A3", 220, true))),
new Author("Author B", true, Arrays.asList(
new Book("B1", 80, true),
new Book("B2", 80, false),
new Book("B3", 190, true),
new Book("B4", 210, true))),
new Author("Author C", true, Arrays.asList(
new Book("C1", 110, true),
new Book("C2", 120, false),
new Book("C3", 130, true))),
new Author("Author D", false, Arrays.asList(
new Book("D1", 200, true),
new Book("D2", 300, false))),
new Author("Author X", true, Collections.emptyList()));
}
}
class Author {
String name;
boolean active;
List<Book> books;
Author(String name, boolean active, List<Book> books) {
this.name = name;
this.active = active;
this.books = books;
}
@Override
public String toString() {
return name + "\t| " + (active ? "Active" : "Inactive");
}
}
class Book {
String name;
int price;
boolean published;
Book(String name, int price, boolean published) {
this.name = name;
this.price = price;
this.published = published;
}
@Override
public String toString() {
return name + "\t| " + "\t| $" + price + "\t| " + (published ? "Published" : "Unpublished");
}
}