I am working on a program that parses a csv file containing a list of movies and data on them such as director name, duration, genres, title, rating, year of release, and IMDB score. The program contains a movie object which sets each of these data points to an attribute. as the majority of the movies have multiple genres(i.e. they can be action, adventure, and fantasy) the attribute "genres" has the data type Set.
I am trying to make multiple lists that identify movies by single genre values such as "adventure", but I cannot figure out how to use a stream to parse the genres set. what I have tried is as follows:
movies.stream().filter((m)->{m.getGenres().contains("Action");}).sorted((m1,m2)-> {Double.compare(m2.getImdbScore(), m1.getImdbScore());}).limit(10).collect(Collectors.toList());
I keep getting the following error:
The method filter(Predicate<? super Movie>) in the type Stream<Movie> is not applicable for the arguments ((<no type> m) -> {})
. How do I do this correctly?
You're using lambdas wrong. Either you use the curly brackets with returning something or the short form without it.
Try the following:
movies.stream()
.filter(m -> m.getGenres().contains("Action"))
.sorted((m1, m2) -> Double.compare(m2.getImdbScore(), m1.getImdbScore()))
.limit(10)
.collect(Collectors.toList());