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


angel juarez
angel juarez
15,886 Points

Can you add the values of a map to a new ArrayList?

In this method: private Map<String, List<Song>> byArtist(){ Map<String, List<Song>> byArtist = new HashMap<>();

for(Song song : mSongs){
  List<Song> artistSong = byArtist.get(song.getArtist());
  if(artistSong == null){
    artistSong = new ArrayList<>();
    byArtist.put(song.getArtist(), artistSong);
return byArtist;


There's a map in the method and there's a List "artistSong" that takes the value byArtist.get(song.getArtist()). As I understand, the new List is used to store the values of the map byArtist, these values are used to create a new ArrayList. Did I understand it correctly? Can we used a map values to make a new arraylist?

1 Answer

Florian Tönjes
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hi angel,

I'm afraid you understood it wrongly. The new List is not used to store the values of the map. It is used to store all songs by a given artist within the Map.

The method iterates over all songs and checks if the song's artist is not in the map, yet. If he is not in the map it will create a new entry for the artist. This artist entry is then associated with a List of his songs.

Hopefully this helps you.

Kind Regards, Florian