Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Ricardo Sala
Ricardo Sala
16,211 Points

ByArtist method - I don't get it!

First of all, as always, thank you in advance!

So, the thing is: this method is suppose to return a Map<String, List<Song>> with Artist (String) and Song(s) (List of Song>.

here is the code (can somebody please fix it? somehow it doesn't show correctly!):

  private Map<String, List<Song>> byArtist() {
    Map<String, List<Song>> byArtist = new HashMap<String, List<Song>>;

    for (Song song : Songs) {
      List<Song> artistSongs = byArtist.get(song.getArtist());
      if ( artistSongs = null ) {
        artistSongs = new ArrayList<>();
        byArtist.put(song.getArtist(), artistSongs); //PERO SI ESTÁ PONIENDO UN ARRAY VACÍO...
      }
      artistSongs.add(song);
    }
    return byArtist;

  }

The thing is, I DON'T SE WHEN IT SAVES THE SONGS OF THE ARTIST!

I mean, it does artistSong.add(song), but it doesn't assign that new "uploaded" list to the byArtist map!

It works, but I can't see why!

Can some moderator please comment the code so we can understand it step by step?

Thank you!

YOU ROCK!

[MOD: edited code block - srh]