-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMovieTest.java
More file actions
404 lines (278 loc) · 18.5 KB
/
MovieTest.java
File metadata and controls
404 lines (278 loc) · 18.5 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package movieexample;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class MovieTest {
public static List<Movie> movies = MovieUtil.getMovieData();
public static void main(String[] args) {
// Exercise 1 - Which movies were released in 2020
int year = 2020;
movieRelasedInYear(year);
// Exercise 2 - List horror movies
Genre genre = Genre.HORROR;
movieListByGenre(genre);
// Exercise 3 - List Comedy movies released in 2019
int yearCondition = 2019;
listMovies(yearCondition,genre);
// Exercise 4 - Get all movies where Brad Pitt is an actor
String actor = "Brad Pitt";
getAllMoviesByActor(actor);
// Exercise 5 - Get all movies with Brad Pitt but not with Leonardo DiCaprio
String notequalActor = "Leonardo DiCaprio";
getAllMoviesByActorWithoutAnotherActor(actor,notequalActor);
// Exercise 6 - Get top rated movies (5 stars)
int star = 5;
getTopRatedMovies(star);
// Exercise 7 - Get top rated movies in 2020
getTopRatedMoviesByYear(year,star);
// Exercise 8 - Group movies by genre
groupMoviesByGenre();
List<String> movies = getActionMovieNames();
System.out.println("\nExercise 9 - Names of Action Movies: "+movies);
// Exercise 9 - Names of Action Movies: [Casino Royale, Fight Club, Bad Boys For Life, Rocky Two, Rocky, Rocky Three]
List<String> actionMoviesSortedByName = getActionMovieNamesSorted();
System.out.println("\nExercise 10 - Names of Action movies sorted: "+actionMoviesSortedByName);
// Exercise 10 - Names of Action movies sorted: [Bad Boys For Life, Casino Royale, Fight Club, Rocky, Rocky Three, Rocky Two]
List<Movie> actionMoviesByReleaseDate = getActionMovieSortedByReleaseDate();
System.out.println("\nExercise 11 - Action movies sorted by release date: "+actionMoviesByReleaseDate);
// Exercise 11 - Action movies sorted by release date: [Movie [name=Rocky, dateOfRelease=1976-12-03], Movie [name=Rocky Two, dateOfRelease=1979-06-15], Movie [name=Rocky Three, dateOfRelease=1982-05-28], Movie [name=Fight Club, dateOfRelease=1999-10-10], Movie [name=Casino Royale, dateOfRelease=2006-11-14], Movie [name=Bad Boys For Life, dateOfRelease=2020-01-17]]
List<String > directors = getDirectorNamesOfMoviesAfter2000();
System.out.println("\nExercise 12 - Names of directors that have released movies "
+ "after year 2000: " +directors);
// Exercise 12 - Names of directors that have released movies after year 2000: [Martin Campbell, Anthony Russo, Peter Howitt, Martin Scorsese, Nicolas Pesce, Quentin Tarantino, Adil El Arbi]
// Exersise 13 : Show First Rocky Movie
findFirstRockyMovie();
// Exersise 14 : Show statement if Will Smith is a cast of any movie in 2020
willSmithMovie2020();
//Exercise 15 - Find the highest grossing movie.
Optional<Movie> highestEarningMovie = highestGrossingMovie();
System.out.println("Exercise 15 - Find the highest grossing movie : " + highestEarningMovie);
// Exercise 15 - Find the highest grossing movie : Optional[Movie [name=Avengers:Endgame, dateOfRelease=2019-04-26]]
//Exercise 16 - find the lowest grossing movie in the year 2020
Optional<Movie> lowestEarningMovieIn2020 = lowestGrossingMovieIn2020();
System.out.println("Exercise 16 - find the lowest grossing movie in the year 2020 : " + lowestEarningMovieIn2020);
// Exercise 16 - find the lowest grossing movie in the year 2020 : Optional[Movie [name=The Grudge, dateOfRelease=2020-01-03]]
//Exercise 17 - Total money - films made by Martin Scorsese
long sum = sumGrossMoviesByMartinScorsese();
System.out.println("Exercise 17 - Total money - films made by Martin Scorsese : " + sum);
// Exercise 17 - Total money - films made by Martin Scorsese : 683000000
//Exercise 18 - How many movies released in last 5 years ?
long noLastFiveYears = noOfMoviesInLastFiveYears();
System.out.println("Exercise 18 - How many movies released in last 5 years ? : " + noLastFiveYears);
// Exercise 18 - How many movies released in last 5 years ? : 4
//Exercise 19 - group movies by genre
Map<Genre,List<Movie>> movieByGenre = groupByGenre();
System.out.println("Exercise 19 - group movies by genre : " + movieByGenre);
// Exercise 19 - group movies by genre : {SUPERHERO=[Movie [name=Avengers:Endgame, dateOfRelease=2019-04-26]], COMEDY=[Movie [name=Johnny English, dateOfRelease=2003-04-11], Movie [name=Once upon a time in Hollywood, dateOfRelease=2019-07-26]], HORROR=[Movie [name=The Grudge, dateOfRelease=2020-01-03]], ACTION=[Movie [name=Casino Royale, dateOfRelease=2006-11-14], Movie [name=Fight Club, dateOfRelease=1999-10-10], Movie [name=Bad Boys For Life, dateOfRelease=2020-01-17], Movie [name=Rocky Two, dateOfRelease=1979-06-15], Movie [name=Rocky, dateOfRelease=1976-12-03], Movie [name=Rocky Three, dateOfRelease=1982-05-28]], CRIME=[Movie [name=The Departed, dateOfRelease=2006-09-26], Movie [name=The Wolf Of Wall Street, dateOfRelease=2013-12-25]]}
//Exercise 20 - Find the number of movies released in each year
Map<Integer,Long> groupByYearCount = totalMoviesReleasesInEachYear();
System.out.println("Exercise 20 - Find the number of movies released in each year : " + groupByYearCount);
// Exercise 20 - Find the number of movies released in each year : {2003=1, 2019=2, 2020=2, 2006=2, 1976=1, 1979=1, 2013=1, 1982=1, 1999=1}
//Exercise 21 - Find the gross earning of movies classified by genre
Map<Genre,Long> grossByGenre = grossEarningByGenre();
System.out.println("Exercise 21 - Find the gross earning of movies classified by genre : " + grossByGenre);
// Exercise 21 - Find the gross earning of movies classified by genre : {SUPERHERO=3000000000, COMEDY=533000000, HORROR=50000000, ACTION=1693099584, CRIME=683000000}
//Exercise 22 - Group movies with rating >= 4
Map<Integer,List<Movie>> groupByRating = groupByFourAndAboveRating();
System.out.println("Exercise 22 - Group movies with rating >= 4 : " + groupByRating);
// Exercise 22 - Group movies with rating >= 4 : {4=[Movie [name=Casino Royale, dateOfRelease=2006-11-14], Movie [name=Johnny English, dateOfRelease=2003-04-11], Movie [name=Rocky Two, dateOfRelease=1979-06-15], Movie [name=Rocky, dateOfRelease=1976-12-03], Movie [name=Rocky Three, dateOfRelease=1982-05-28]], 5=[Movie [name=Avengers:Endgame, dateOfRelease=2019-04-26], Movie [name=The Departed, dateOfRelease=2006-09-26], Movie [name=The Wolf Of Wall Street, dateOfRelease=2013-12-25], Movie [name=Once upon a time in Hollywood, dateOfRelease=2019-07-26], Movie [name=Fight Club, dateOfRelease=1999-10-10]]}
//Exercise 23 - Group movies with rating >= 4 but only names of movies.
Map<Integer,List<String>> byRatingMovieName = movieNamesGroupedByRating();
System.out.println("Exercise 23 - Group movies with rating >= 4 but only names of movies : " + byRatingMovieName);
// Exercise 23 - Group movies with rating >= 4 but only names of movies : {4=[Casino Royale, Johnny English, Rocky Two, Rocky, Rocky Three], 5=[Avengers:Endgame, The Departed, The Wolf Of Wall Street, Once upon a time in Hollywood, Fight Club]}
//Exercise 24: Find list of stars who have acted in Martin Scorsese movies
List<String> starCast = starsInMartinMovies();
System.out.println("Exercise 24: Find list of stars who have acted in Martin Scorsese movies : " + starCast);
// Exercise 24: Find list of stars who have acted in Martin Scorsese movies : [Leonardo DiCaprio, Matt Damon, Jack Nicholson, Leonardo DiCaprio, Jonah Hill, Margot Robbie]
//Exercise 25: Find unique list of stars who have acted in Martin Scorsese movies
Set<String> starCastUnique = uniqueStarsInMartinMovies();
System.out.println("Exercise 25: Find unique list of stars who have acted in Martin Scorsese movies : " + starCastUnique);
// Exercise 25: Find unique list of stars who have acted in Martin Scorsese movies : [Jonah Hill, Leonardo DiCaprio, Matt Damon, Jack Nicholson, Margot Robbie]
}
public static List<String> starsInMartinMovies() {
return movies.stream()
.filter(movie -> movie.getDirectorName().equals("Martin Scorsese"))
.flatMap(movie -> movie.getCast().stream())
.collect(Collectors.toList());
}
public static Set<String> uniqueStarsInMartinMovies() {
return movies.stream()
.filter(movie -> movie.getDirectorName().equals("Martin Scorsese"))
.flatMap(movie -> movie.getCast().stream())
.collect(Collectors.toSet());
}
public static Map<Genre, Long> grossEarningByGenre() {
return movies.stream()
.collect(Collectors.groupingBy(
Movie::getGenre,
Collectors.summingLong(Movie::getGrossEarning)
)
);
}
public static Map<Genre,List<Movie>> groupByGenre() {
return movies.stream()
.collect(Collectors.groupingBy(Movie::getGenre));
}
public static Map<Integer, Long> totalMoviesReleasesInEachYear() {
return movies.stream()
.collect(Collectors.groupingBy((Movie movie) ->
movie.getDateOfRelease().getYear(),
Collectors.counting()
)
);
}
public static Map<Integer, List<Movie>> groupByFourAndAboveRating() {
return movies.stream()
.filter( movie -> movie.getRating() >=4)
.collect(Collectors.groupingBy(Movie::getRating));
}
public static Map<Integer, List<String>> movieNamesGroupedByRating() {
return movies.stream()
.filter(movie -> movie.getRating()>=4)
.collect(Collectors.groupingBy
(
Movie::getRating,
Collectors.mapping(Movie::getName, Collectors.toList())
)
);
}
private static long noOfMoviesInLastFiveYears() {
LocalDate lastFiveYears = LocalDate.now().minusYears(5);
return movies.stream()
.filter(movie -> movie.getDateOfRelease().isAfter(lastFiveYears))
.count();
}
public static Optional<Movie> highestGrossingMovie() {
Comparator<Movie> byGrossEarning = Comparator.comparing(Movie::getGrossEarning);
return movies.stream()
.max(byGrossEarning);
}
public static Optional<Movie> lowestGrossingMovieIn2020() {
Comparator<Movie> byGrossEarning = Comparator.comparing(Movie::getGrossEarning);
return movies.stream()
.filter(movie -> movie.getDateOfRelease().getYear() == 2020)
.min(byGrossEarning);
}
public static long sumGrossMoviesByMartinScorsese() {
return movies.stream()
.filter(movie -> movie.getDirectorName().equals("Martin Scorsese"))
.mapToLong(movie -> movie.getGrossEarning())
.sum();
}
public static void findFirstRockyMovie() {
Comparator<Movie> byReleaseDate = Comparator.comparing(Movie::getDateOfRelease);
Optional<Movie> movie = movies.stream()
.filter(m -> m.getName().contains("Rocky"))
.sorted(byReleaseDate)
.findFirst();
System.out.println(movie.orElseThrow());
// Movie [name=Rocky, dateOfRelease=1976-12-03]
}
public static void willSmithMovie2020() {
boolean willSmithMovie2020 =
movies.stream()
.filter(movie -> movie.getDateOfRelease().getYear() == 2020)
.anyMatch(movie -> movie.getCast().contains("Will Smith"));
System.out.println(willSmithMovie2020);
// true
}
//exercise 9 - Using map
public static List<String> getActionMovieNames() {
return movies.stream()
.filter(movie -> movie.getGenre().equals(Genre.ACTION))
.map(movie -> movie.getName())
.collect(Collectors.toList());
}
//exercise 10- using sorted- sort action movies by name
public static List<String> getActionMovieNamesSorted() {
return movies.stream()
.filter(movie -> movie.getGenre().equals(Genre.ACTION))
.map(movie -> movie.getName())
.sorted()
.collect(Collectors.toList());
}
//exercise 11- using sorted- sort action movies by release date
public static List<Movie> getActionMovieSortedByReleaseDate() {
Comparator<Movie> byReleaseDate = Comparator.comparing(Movie::getDateOfRelease);
return movies.stream()
.filter(movie -> movie.getGenre().equals(Genre.ACTION))
.sorted(byReleaseDate)
.collect(Collectors.toList());
}
//exercise 12 - Retrieve the names of directors that have released movie after year 2000
public static List<String> getDirectorNamesOfMoviesAfter2000() {
return movies.stream()
.filter(movie -> movie.getDateOfRelease().getYear() > 2000)
.map(movie -> movie.getDirectorName())
.distinct()
.collect(Collectors.toList());
}
private static void groupMoviesByGenre() {
Map<Genre,List<Movie>> movieByGenre = new HashMap<>();
movies.stream().forEach(
movie -> movieByGenre.computeIfAbsent(
movie.getGenre(), k -> new ArrayList<Movie>()
).add(movie)
);
System.out.println("\nExercise 8 - groupMoviesByGenre : "+movieByGenre);
// Exercise 8 - groupMoviesByGenre : {SUPERHERO=[Movie [name=Avengers:Endgame, dateOfRelease=2019-04-26]], COMEDY=[Movie [name=Johnny English, dateOfRelease=2003-04-11], Movie [name=Once upon a time in Hollywood, dateOfRelease=2019-07-26]], HORROR=[Movie [name=The Grudge, dateOfRelease=2020-01-03]], ACTION=[Movie [name=Casino Royale, dateOfRelease=2006-11-14], Movie [name=Fight Club, dateOfRelease=1999-10-10], Movie [name=Bad Boys For Life, dateOfRelease=2020-01-17], Movie [name=Rocky Two, dateOfRelease=1979-06-15], Movie [name=Rocky, dateOfRelease=1976-12-03], Movie [name=Rocky Three, dateOfRelease=1982-05-28]], CRIME=[Movie [name=The Departed, dateOfRelease=2006-09-26], Movie [name=The Wolf Of Wall Street, dateOfRelease=2013-12-25]]}
}
private static void getTopRatedMoviesByYear(int year, int star) {
List<Movie> topRated2020 = getMovies(movieWithRating(star).and(movieReleasedInYear(year)));
System.out.println("\nExercise 7 -Top rated in 2020 : " +topRated2020);
// Exercise 7 -Top rated in 2020 : []
}
private static void getTopRatedMovies(int star) {
Predicate<Movie> topRated = movieWithRating(star);
List<Movie> topRatedMovies = getMovies(topRated);
System.out.println("\nExercise 6 - Get top rated movies : "+topRatedMovies);
// Exercise 6 - Get top rated movies : [Movie [name=Avengers:Endgame, dateOfRelease=2019-04-26], Movie [name=The Departed, dateOfRelease=2006-09-26], Movie [name=The Wolf Of Wall Street, dateOfRelease=2013-12-25], Movie [name=Once upon a time in Hollywood, dateOfRelease=2019-07-26], Movie [name=Fight Club, dateOfRelease=1999-10-10]]
}
private static void getAllMoviesByActorWithoutAnotherActor(String actor,String notequalActor) {
Predicate<Movie> moviesWithBradPitt = movieByStarCast(actor);
Predicate<Movie> moviesWithLeo = movieByStarCast(notequalActor);
List<Movie> moviesWithBradAndNoLeo = getMovies(moviesWithLeo.negate().and(moviesWithBradPitt));
System.out.println("\nExercise 5 - Brad without Leo moves : "+moviesWithBradAndNoLeo);
// Exercise 5 - Brad without Leo moves : [Movie [name=Fight Club, dateOfRelease=1999-10-10]]
}
private static void getAllMoviesByActor(String actor) {
Predicate<Movie> moviesWithBradPitt = movieByStarCast(actor);
List<Movie> movieListBradPitt= getMovies(moviesWithBradPitt);
System.out.println("\nExercise 4 - Brad Pitt stars in the following : "+movieListBradPitt);
// Exercise 4 - Brad Pitt stars in the following : [Movie [name=Once upon a time in Hollywood, dateOfRelease=2019-07-26], Movie [name=Fight Club, dateOfRelease=1999-10-10]]
}
private static void listMovies(int yearCondition, Genre genre) {
Predicate<Movie> horrorInYear = movieReleasedInYear(yearCondition).and(movieByGenre(genre));
List<Movie> horrorMoviesIn2019 = getMovies(horrorInYear);
System.out.println("\nExercise 3 - Horror movies in the year 2019: "+horrorMoviesIn2019);
// Exercise 3 - Horror movies in the year 2019: []
}
private static void movieListByGenre(Genre genre) {
Predicate<Movie> horrorMovies = movieByGenre(genre);
List<Movie> getHorrorMovies = getMovies(horrorMovies);
System.out.println("\nExercise 2 - Horror movies: " +getHorrorMovies);
// Exercise 2 - Horror movies: [Movie [name=The Grudge, dateOfRelease=2020-01-03]]
}
private static void movieRelasedInYear(int year) {
Predicate<Movie> movieReleasedInYear2020 = movieReleasedInYear(year);
List<Movie> getMoviesInYear = getMovies(movieReleasedInYear2020);
System.out.println("\nExercise 1 - Movies released in Year "+ year + ": " + getMoviesInYear);
// Exercise 1 - Movies released in Year 2020: [Movie [name=The Grudge, dateOfRelease=2020-01-03], Movie [name=Bad Boys For Life, dateOfRelease=2020-01-17]]
}
private static List<Movie> getMovies(Predicate<Movie> movieFilter) {
return movies.stream().filter(movieFilter).collect(Collectors.toList());
}
private static Predicate<Movie> movieReleasedInYear(int year){
return movie -> movie.getDateOfRelease().getYear() == year;
}
private static Predicate<Movie> movieByGenre(Genre genre){
return movie -> movie.getGenre() == genre;
}
private static Predicate<Movie> movieByStarCast(String star){
return movie -> movie.getCast().contains(star);
}
private static Predicate<Movie> movieWithRating(int rating){
return movie -> movie.getRating() == rating;
}
}