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

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


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