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! Changing Course

Brody Ricketts
Brody Ricketts
15,612 Points

Changing Course Challenge Task 2 of 3. Looking for a reference point.

Changing Course.

Craig Dennis, is Custom Serialization time 11:10, the video you are referencing for this challenge ?

I was going over Rob Bridges forum post, where the answer is given, but I don't want the answer, I want to understand how to get to the answer.

Mostly just looking for a point in the right direction.

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Brody!

I think it's more along the lines of the part in Maps where we make the Treets by Author. Circa 11:40.

Let me know if that helps!

Brody Ricketts
Brody Ricketts
15,612 Points
public Map<String, Video> videosByTitle(Course course) {
    // List<Video> videoList = course.getVideos(); Classic case of DRY...
    Map<String, Video> videoMap = new TreeMap<>();
    for (Video video : course.getVideos()) {
      videoMap.put(video.getTitle(), video);
    }
    return videoMap;
  }

This is the code i got to pass challenge 2. I think I am still a little confused on the use of the for loop.

I understand the code, just about every line, except the use of the for loop, most specifically the parameters that are passed into the for loop. Maybe that is why I was having such an issue with this challenge.

Craig Dennis
Craig Dennis
Treehouse Teacher

The for-each loop works like this.... for each thing in things, or for every single item in the grouping of items, do these lines of code.

So course.getVideos() returns a list of videos, and each time the code in between the { run each video is in context, when the code finishes, the next one shows up. It loops as many times as there are things, and works on one at a time.

That make sense?

Brody Ricketts
Brody Ricketts
15,612 Points

If I am understanding this correctly, the map that is created, videoMap, the for-each loop is placing (.put()) each video into the map by title (the key) and the video location (the value), and sorting them alphabetically because we declared it to be a TreeMap.

If I were to recall your explanation of a HashMap, a dictionary.

In this case it would be "Video Title" - "Location of the video". Does it specifically call a database? Such as a text file you had demonstrated for the Karaoke Machine, specifically in Custom Serialization.

Also thank you for providing me the assistance here Craig.

Craig Dennis
Craig Dennis
Treehouse Teacher

Awesome, you got it! It doesn't do any storing. The Map is simply being returned from the method.

Indeed it is basically a dictionary of title to video (where video is an object reference), and TreeMap sorts keys by natural sort order, which is alphabetical. Nailed it!