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

Prasoon Shukla
Prasoon Shukla
3,426 Points

ByArtist method: return byArtist

In the ByArtist method, byArtist Map is getting updated only within if statement. If there already is a list of song mapped to an artist, if statement will not run. How is then byArtist getting updated such that when we do return byArtist, we get all the updated songs?

5 Answers

Nathan Phan
Nathan Phan
9,898 Points

Oh sorry it was my bad for making it difficult to understand.

The return type is indeed a map object (not list, I wasn't being very clear there). But the list is tied to the map (when you call the list using map.get(key), it returns the list of that key from the map.

In the if block, the map.put(key, list) method adds the list to that specific key

That way, when you update the list, the map gets updated too.

Nathan Phan
Nathan Phan
9,898 Points

Your assumption is correct, that the if statement block would not run, but then the program would move to the next line:

artistSongs.add(song);

This line would add the song to the list (which either already existed before or just created during this iteration). This command line happens outside the if block but is still inside the loop.

Prasoon Shukla
Prasoon Shukla
3,426 Points

Thanks Nathan for your reply. I understand now that artistSong would update with a new song. But from here how does program know it needs to feed this new information into byArtist hashmap? Is it like after end of 'for' loop everytime, the hashmap gets updated? I am not clear on where that is happening in the program and in the flow of events.

Nathan Phan
Nathan Phan
9,898 Points

Here's how the method works:

  1. Creates a map, which takes a string as a key value (artists- unique) and for each artist, a list of songs which associate with their name

  2. Loops thru every entity mSong:

    -Get the list of song from that artist using map (we can find the artist name using the song)

    -If the list doesn't exist, creates a new one and ties it to the artist (the list is empty in this case)

    -Add the song to the list, whether empty or not

    -Start the loop again for the next song till no more songs

  3. Return updated list.

Prasoon Shukla
Prasoon Shukla
3,426 Points

But what is getting returned is of Map type, not list (return ByArtist). I am trying to understand that when list is being updated, how is that resulting into updation of map too? Is it like updation of list automatically results in updation of map too ans we don't need to call this out separately?

Prasoon Shukla
Prasoon Shukla
3,426 Points

Thanks! So I understand it now.

Liron Tal
Liron Tal
4,825 Points

byArtist() is not working as expected, I tried 3 different songs of 2 artist, and artistSongs size was only 2.