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 - Retired Efficiency! Implement Chooser UI

Athough this question has been asked many times...i still have my doubts if i'm getting it right or not!!

  private Map<String>,List<Song>> byArtist(){
    Map<String,List<Song>> byArtist=new Hashmap<>(); //new Hashmap
    for(Song song:mSongs){ //parsing through each song
      List<Song> artistSongs=byArtist.get(song.getArtist()); /*checking if the map has the particular artist...
artistSongs references to a song by a particular artist---byArtist.get(song.getArtist())??...
if so why not take a simple String object since we are parsing songs one at a time?*/  

       if(artistSongs==null){ //if the map does not have the artist i.e artistSongs points to null
        artistSongs=new ArrayList<>(); //initialize a new arraylist with the object reference
        byArtist.put(song.getArtist(),artistSongs); //add the artist and his song to the map
      }
      artistSongs.add(song);  //why storing this song in the list if its already there in the map...why not simply use continue and move to the next song??
    }
  return byArtist;

  }

Edit: Can somebody please help!!

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Remember it's a key value store. The key is the artists name and the value is a list of songs. So, there will be one artist name for the key and multiple songs for each artist. It seems that maybe you aren't seeing the bit that an artist can sing multiple songs.

artistSongs.add(song); //why storing this song in the list if its already there in the map...why not simply use continue and move to the next song??

The loop we are processing are unique songs. They should not be duplicates, so therefore they do not exist in the list (the value portion of the map) yet. We turn the artistSongs list into a new list if it doesn't exist.

//add the artist and his song to the map

This is not true, this is the reference to the new empty list that we created.

So again:

artistSongs.add(song); //why storing this song in the list if its already there in the map...why not simply use continue and move to the next song??

artistsSongs is either a populated list from before or a newly created list. The song that we are currently processing in the loop is not in that list in either case, whether it be a new list or an existing one, so we add it.

That help clear things up? Remember our map is keyed artistName to a value that is the list of their songs.

Thank you Craig...that more than clears it up!!

But why do you add it if you are not going to use it in either case?

Craig Dennis
Craig Dennis
Treehouse Teacher

The method returns the map. Someone else will use it. (You will in the next task ;) )