Jiawei Hu9,740 Points
Question regarding byArtist method.
In the case of
artistSongs == null, we do:
artistSongs = new ArrayList<>();
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
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
You seem to have missed a line of code that was added. At 6:12 Craig adds the following line right below the
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.