Created
June 23, 2015 18:19
-
-
Save valdo404/b220df6342efe21b8005 to your computer and use it in GitHub Desktop.
playlist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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