-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamCollectorSolution.java
More file actions
154 lines (114 loc) · 4.66 KB
/
StreamCollectorSolution.java
File metadata and controls
154 lines (114 loc) · 4.66 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.*;
import java.util.stream.*;
public class StreamCollectorSolution
{
// Example Classes Lab Questions
record Book(String title, String author, int releaseYear){}
record Movie(String title, String director, int releaseYear){}
// Instance Lists
List<Book> books;
List<Movie> movies;
// Q1.a Constructor
public StreamCollectorSolution(List<Book> books, List<Movie> movies)
{
this.books = books;
this.movies = movies;
}
// Q1.b Getters
public Stream<Book> getBookStream()
{
return books.stream();
}
public Stream<Movie> getMovieStream()
{
return movies.stream();
}
// Q2 Partitions By Release Year
public List<Book> PartitionBooksByReleaseYear(int partitionYear)
{
Map<Boolean, List<Book>> partitionedBooks = getBookStream()
.collect(Collectors.partitioningBy(book -> book.releaseYear > partitionYear));
// Print the partitions
System.out.println("Contemporary books");
partitionedBooks.get(true).forEach(System.out::println);
System.out.println("");
System.out.println("Classics");
partitionedBooks.get(false).forEach(System.out::println);
List<Book> contemporaryBooks = partitionedBooks.get(true);
return contemporaryBooks;
}
public List<Movie> PartitionMoviesByReleaseYear(int partitionYear)
{
Map<Boolean, List<Movie>> partitionedMovies = getMovieStream()
.collect(Collectors.partitioningBy(movie -> movie.releaseYear > partitionYear));
// Print the partitions
System.out.println("Contemporary movies");
partitionedMovies.get(true).forEach(System.out::println);
System.out.println("");
System.out.println("Classics");
partitionedMovies.get(false).forEach(System.out::println);
List<Movie> contemporaryMovies = partitionedMovies.get(true);
return contemporaryMovies;
}
// Q3 Movies Based on Books
public List<Movie> MoviesBasedOnBooks()
{
System.out.println("\nMovies Based on Books\n");
List<Movie> moviesBasedOnBooks = getMovieStream()
.filter(movie -> getBookStream()
.anyMatch(book -> movie.title.contains(book.title)))
.collect(Collectors.toList());
moviesBasedOnBooks.forEach(System.out::println);
return moviesBasedOnBooks;
}
// Q4 Grouping by creator
public Map<String, List<Movie>> GroupMoviesByDirector()
{
Map<String, List<Movie>> moviesByDirector = getMovieStream()
.collect(Collectors.groupingBy(movie -> movie.director));
moviesByDirector.forEach((director, movieList) -> {
System.out.println(director + ": " + movieList);
});
return moviesByDirector;
}
public Map<String, List<Book>> GroupBooksByAuthor()
{
Map<String, List<Book>> booksByAuthor = getBookStream()
.collect(Collectors.groupingBy(book -> book.author));
booksByAuthor.forEach((author, bookList) -> {
System.out.println(author + ": " + bookList);
});
return booksByAuthor;
}
// Main
public static void main(String args [])
{
// List of books
List<Book> books = List.of(
new Book("Murder on the Orient Express", "Agatha Christie", 1934),
new Book("Death on the Nile", "Agatha Christie", 1937),
new Book("Frankenstein", "Mary Shelly", 1818),
new Book("Dracula", "Bram Stoker", 1896),
new Book("Minor Detail", "Adania Shibli", 2017),
new Book("Septology", "Jon Fosse", 2019),
new Book("Dune", "Frank Herbert", 1965)
);
// List of movies
List<Movie> movies = List.of(
new Movie("Murder on the Orient Express", "Kenneth Branagh", 2017),
new Movie("Frankenstein", "James Whale", 1932),
new Movie("Dracula", "Francis Ford Coppola", 1993),
new Movie("Apocolypse Now", "Francis Ford Coppola", 1979),
new Movie("The Pianist", "Roman Polanski", 2002),
new Movie("Dune", "David Villeneuve", 2021),
new Movie("Arrival", "David Villeneuve", 2016)
);
System.out.print("\n\nMain Function : Exercise 2\n\n");
StreamCollectorSolution collector = new StreamCollectorSolution(books, movies);
collector.PartitionBooksByReleaseYear(2003); // Q2.1
collector.PartitionMoviesByReleaseYear(2003); // Q2.2
collector.MoviesBasedOnBooks(); // Q3
collector.GroupMoviesByDirector(); // Q4.1
collector.GroupBooksByAuthor(); // Q4.2
}
}