Java Java Data Structures Efficiency! Custom Serialization

Taha Abbasi
Taha Abbasi
12,862 Points

The method sort(new Comparator<Song>() {}) is undefined for type List<Song>

I am getting the below error when sorting the Songs alphabetically.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method sort(new Comparator<Song>(){}) is undefined for the type List<Song>

    at com.teamtreehouse.model.SongBook.getSongsForArtist(SongBook.java:88)
    at com.teamtreehouse.KaraokeMachine.promptSongForArtist(KaraokeMachine.java:101)
    at com.teamtreehouse.KaraokeMachine.run(KaraokeMachine.java:61)
    at Karaoke.main(Karaoke.java:13)

Here is my 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;
    }

Any suggestions?

Craig Dennis
Craig Dennis
Treehouse Teacher

Can you show me all the code in that file please? Not seeing anything to out of place there. Assuming it might be your byArtist method....

Taha Abbasi
Taha Abbasi
12,862 Points

Hi Craig,

Here is the full code from the file, One thing I did differently is I declared the type in both the Interface and the Implementation of the Map and List we are creating in the byArtist method. Maybe I'm messing something up there? but this works if I take out the sorting of the songs and revert back to the previous versions where songs are not sorted but only artist names are.

package com.teamtreehouse.model;

import java.util.ArrayList;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.TreeMap;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
import java.io.PrintWriter;
import java.util.Set;

public class SongBook {
    private List<Song> mSongs;

    // SongBook array list to hold songs
    public SongBook() {
        mSongs = new ArrayList<Song>();
    }

    public void exportTo(String fileName) {
        try (
                FileOutputStream fos = new FileOutputStream(fileName);
                PrintWriter writer = new PrintWriter(fos);
        ) {

                for (Song song : mSongs) {
                    writer.printf("%s|%s|%s%n",
                                song.getArtist(),
                                song.getTitle(),
                                song.getVideoURL());
                }
            } catch(IOException ioe) {
                System.out.printf("Problem saving %s %n", fileName);
                ioe.printStackTrace();
            }
        }


    public void importFrom(String fileName) {
        try ( 
                FileInputStream fis = new FileInputStream(fileName);
                BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
        ) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] args = line.split("\\|");
                addSong(new Song (args[0], args[1], args[2]));
            }
        } catch(IOException ioe) {
            System.out.printf("Problems loading %s %n", fileName);
        }
    }

    // User can add songs to the SongBook array list with this method
    public void addSong(Song song) {
        mSongs.add(song);
    }

    // Can be used to get number of songs in the SongBook ArrayList
    public int getSongCount() {
        return mSongs.size();
    }

    // FIXME: This should be cached!
    private Map<String, List<Song>> byArtist() {
        Map<String, List<Song>> byArtist = new TreeMap<String, List<Song>>();
        for (Song song : mSongs) {
            List<Song> artistSongs = byArtist.get(song.getArtist());
            if (artistSongs == null) {
                artistSongs = new ArrayList<Song>();
                byArtist.put(song.getArtist(), artistSongs);
            }
            artistSongs.add(song);
        }
        return byArtist;
    }

    public Set<String> getArtists() {
        return byArtist().keySet();
    }

    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;
    }
}

2 Answers

Taha Abbasi
Taha Abbasi
12,862 Points

So did some more research on Stackoverflow and found that the I actually have to use Collections.sort(songs, new Comparator<Song>() {....

Issue:

// songs.sort not working;

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;
}

Solution:

// Collections.sort(songs, .... works;

public List<Song> getSongsForArtist(String artistName) {
        List<Song> songs = byArtist().get(artistName);
        Collections.sort(songs, 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;
    }

My files are using Java Compiler version 1.7 instead of 1.8. Do you think that might have something to do with why this is different? This has fixed the problem, but I'm not fully clear on why this is different other than the fact that I'm using a different version of Java. Can you clarify this? I hope this helps someone else though :)