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 Efficiency! Implement Chooser UI

Not clear on behavior of method

Can someone explain to me what is the following method doing? Thanks!

public List<Song> getSongsForArtist(String artistName) {

  return byArtist().get(artistName);

2 Answers

Linda de Haan
Linda de Haan
12,413 Points

I still had the entire project on my computer so I could easily take a look for you. But other people that would want to answer your question might not have it. It's best to post your question as complete as possible. Include all relevent code and use markdown, so people can see exactly what your code does and help you answer your question.

Here's what this code does:

getSongsForArtist(String artistName) returns a List of type Song. byArtist returns a map (key-value set) sorted by artistname (key) and all their songs (value) The get() method after byArtist().get() searches for the specific artist in the map. If it finds the artist it puts his songs to a new List of type Song.

One way to easily find out what code does exactly is to either use the debugger of your IDE or write several System.out.println() with relevant text that explains where in the code it is. For example: System.out.println("In the byArtist() method, entering the for loop"). That way you can follow your code using the output to the console.

Thank you, Linda!

You make a good point on adding relevant text to highlight where the code is, therefore identifying what process is actually take place during program execution.

Thank you very much!