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

Q.can someone help me with this code? private Map<String , List<Song>> byArtist(){ Map<String , List<Song>> byArtist = new HashMap<>(); for( Song song : mSongs) { List<Song> artistSongs = byArtist.get(song.getArtist()); if(artistSongs == null){ artistSongs = new ArrayList<>(); byArtist.put(song.getArtist(),artistSongs); } artistSongs.add(song); } return byArtist; } public Set<String> getArtist() { return byArtist().keySet(); } public List<Song> getSongForArtist(String artistName){ return byArtist().get(artistName); }

lol no, who would be able to understand that.

private Map < String, List < Song >>

 byArtist() {
  Map < String, List < Song >> byArtist = new HashMap < > ();
  for (Song song: mSongs) {
   List < Song > artistSongs = byArtist.get(song.getArtist());
   if (artistSongs == null) {
    artistSongs = new ArrayList < > ();
    byArtist.put(song.getArtist(), artistSongs);
   }
   artistSongs.add(song);
  }
  return byArtist;
 }
public Set < String > getArtist() {
 return byArtist().keySet();
}
public List < Song > getSongForArtist(String artistName) {
 return byArtist().get(artistName);
}

tomandrews there you go. google "java code formatter" if you need help cleaning up a post.

It's almost as quick as lol'ing, and about a thousand times more useful.

1 Answer

Thanks Gavin , Can you please help me out with this code ?