Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
John Cicalese
3,542 PointsJava Data Structures Karaoke Machine getSongsForArtist method
I've been doing the Karaoke Machine project in eclipse versus in workspaces. At the very end of "Custom Serialization" we're making an adjustment to getSongsForArtist(String) method to fix the song order. Craig is able to compile and run fine in workspaces. However in eclipse there's an error with the .sort() method and the error shows as "The method sort(new Comparator<Song>(){}) is undefined for the type List<Song>."
Code: public List<Song> getSongsForArtist(String artistName){ List<Song> songs = byArtist().get(artistName); songs.sort(new Comparator<Song>(){ @Override public int compare(Song song1, Song song2){ if(song1.equals(song2)){ return 0; } return song1.mTitle.compareTo(song2.mTitle); } }); return songs; }
Eclipse's quick fix option wants to cast songs. Any thought on how to fix this?
Thanks, John
1 Answer
Craig Dennis
Treehouse TeacherWorkspaces is using Java 1.8. You should be too ;) List.sort was added in 1.8.
You can use Collections.sort(<list>, new Comparator<Song>() {.... as a stop gap.
Sorry for the confusion!