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

Jiawei Hu
Jiawei Hu
9,740 Points

Question regarding byArtist method.

In the case of artistSongs == null, we do:

artistSongs = new ArrayList<>();

(now artistSongs is just an empty ArrayList that has No songs in it), and then we put both the artist and this empty ArrayList of this element into the byArtist() map and then we go for the next element of the mSongs SongBook.

The matter is that we never really manage to put this song into the Map or the artistSongs ArrayList and all we did it just create a map element with key equals the author of this song and an empty ArrayList as value.

Isn't it NOT what we want? Isn't there a line of code missing where it actually adds the song to the newly created artistSongs ArrayList?

1 Answer

andren
andren
28,558 Points

You seem to have missed a line of code that was added. At 6:12 Craig adds the following line right below the if statement:

artistSongs.add(song);

Which adds the song to the list. This line gets executed both when the if statement is hit and when it is not. So the song is always added to the list.