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

Melissa Shields
Melissa Shields
7,108 Points

Code Challenge Task #2: I don't understand exactly what is being mapped in this task.

Can someone please help me understand, in plain English, exactly what the key-value pairs contained in the map represent? I understand the mechanics of the code required.

What is confusing me is the fact that the Video constructor takes only 1 parameter - a string representing the video title. So, when we create the map, mapping the video title to the "video", what exactly are we mapping the video title to?

Thanks very much!

4 Answers

Jordan Ernst
Jordan Ernst
5,121 Points

what Grigorij Schleifer is saying is when using a Map<Key, Value> you have a key and a value because or Map is requiring a <String, Video> object we need to pass those values. vid.getTitle()// returns a String, vid// returns a Video object which we need. in the for loop we are basically saying hey videoMap put this darn title that we got from this getTitle() method into your key factory and that key is Valuable to me because it opens up my magic box Collection of Videos. weird way of putting it but it may be useful.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Melissa,

You create a Map with two "parameters"

    Map<String, Video> byTitle = new TreeMap<String, Video>();
    // create am Map and declare it as TreeMap to be ordered alphabetically
    // that will give you a natural ordered collection of Titles and Videos inside the Map
    // Title as key and the Video as value

The String inside the Map is the key and it is our Title. To every key (Title) you have a corresponding value (Video) ...

or in other words:

    Map<key, value> byTitle = new TreeMap<key, value>();

or in pseudocode:

    Map<Title of the Video as key, Video itself as value> byTitle = new TreeMap<key, value>();

To your second question:

To create a Video object our Video contructor takes the title of the Video. And it is a String.

Was this helpful?

Grigorij

Melissa Shields
Melissa Shields
7,108 Points

Grigorij, thanks for your response. I'm probably missing something very basic here, but unfortunately I still don't understand what the "Video itself as value" in the map represents. When we create a new video using the constructor we pass in only 1 parameter, a title. So when we map that video title to the "Video itself as value" it seems as though we're mapping the title to nothing because there's no other information about the video that was passed in as a parameter when the video was created - no video URL or video creator, for example. So, it's not clear to me what 2 pieces of information we're linking together in the map - the video title and ???

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Melissa,

sorry for the delay.

You can order Videos in a Map and "assign" to every Title (your key) the Video as the value. Your Video class constructor takes the Title "My fancy title" and creates a Video object with that title. So you are not mapping a Title to nothing. you are combining the Video with the Title during the object creation. There is no Map here ... because you haven´t created the Map yet.

Let us look at the complete videoByTitle method:

 public Map<String, Video> videosByTitle(Course course) {
        // Here you create a Map 
       // and defining the key and value
       // there is still nothing mapped to the Map
        Map<String, Video> videoMap = new HashMap<String, Video>();
        List<Video> vids = course.getVideos();
        for (Video vid : vids) {
          videoMap.put(vid.getTitle(), vid);
        // HERE you map the Title and the Video together into the Map
       // so now the title is mapped to the Video inside the Map
        }
        return videoMap;
      }

Makes sense?

Grigorij