Skip to content

Instantly share code, notes, and snippets.

@valdo404
Created June 23, 2015 18:19
Show Gist options
  • Save valdo404/b220df6342efe21b8005 to your computer and use it in GitHub Desktop.
Save valdo404/b220df6342efe21b8005 to your computer and use it in GitHub Desktop.
playlist
public class PlaylistParser
{
/**
* @param playlistContent a string representation of the playlist content
* @return a list of string representation of a song
*/
public List getFavoriteSongs(final String playlistContent) {
final List<Optional<Song>> songs = parseSongs(playlistContent);
return bestSongs(computeScores(songs));
}
private List<Song> bestSongs(Map<Song, Integer> scores) {
return scores.entrySet().stream().parallel().
sorted(scoreComparator()).limit(5).
map(Entry::getKey).
collect(toList());
}
private Map<Song, Integer> computeScores(List<Optional<Song>> songs) {
return songs.stream().
filter(Optional::isPresent).
map(Optional::get).
collect(groupingBy(identity(), summingInt(song -> 1)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment