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 Java Data Structures Efficiency! Implement Chooser UI

Not understood this part

public Map<String , List<Song>> byArtist(){
    Map<String , List<Song>> byArtist.get(song.getArtist());
        for(Song song : mSongs ){
      List<Song> artistSongs = byArtist.get(song.getArtist());
        if(artistSongs == null){
          artistSongs = new ArrayList<>();
          byArtist.put(song.getArtist(), artistSongs);
        }
        artistSongs.add(song); 
    }     return byArtist;
    }

in this what will be output when artistSong == null , Will it print all the songs

and byArtist.put(song.getArtist(), artistSongs); what will this code do

O.K. --> use JShell - it can really help you understand this. On the beginning List<Song> artistSongs is null (you didn't use "new" to point it to the object so its only null reference to nothing). Moreover

byArtist.get(song.getArtist()); 

returns empty (null) value for key (Artist) so the next step is to create artistSongs list for this Artist. Then

byArtist.put(song.getArtist(), artistSongs);

in map byArtist is been placed name of current artist from current song as a key and just created empty artistSongs list as a value.

If name of Artist repeat in next loop "if" condition statement will be ignored because artistSong of current artist will have value already and not returns null. Only if this statement will be given brand new Artist this variable will get null again.

I have written more here: >>>> https://teamtreehouse.com/community/regarding-the-difficulty-of-this-video