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

Where did I go wrong here? :/

I've tried this a couple ways already. What am I missing?

com/example/model/Course.java
package com.example.model;

import java.util.List;

public class Course {
  private String mName;
  private List<Video> mVideos; 

  public Course(String name, List<Video> videos) {
    mName = name;
    mVideos = videos;
    for (Course video : videos)  {
     videosByTitle.add(video.getVideo());

    }
  }

  public String getName() {
    return mName;
  }

  public List<Video> getVideos() {
    return mVideos;
  }


}
com/example/model/Video.java
package com.example.model;

public class Video {
  private String mTitle;

  public Video(String title) {
    mTitle = title;
  }

  public String getTitle() {
    return mTitle;
  }

  public void setTitle(String title) {
    mTitle = title;
  }

}
QuickFix.java
import com.example.model.Course;
import com.example.model.Video;

import java.util.Map;

public class QuickFix {

  public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
Video video = new Video("The Beginning Bits"); 
    // TODO(2):  Add the newly created video to the course videos as the second video.
course.getVideos().add(1, video); 
  }

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {

  }

  public Map<String, Video> videosByTitle(Course course) {
    return null;
  }

}

You seem to get stuck at task 2, videosByTitle, is that correct? What have you tried already?

someone tried to help me with this bit of info, but it is so confusing! I feel like this could be explained more simply. :/

"Okay, so if you look in the Course class, you'll see that it has a List of Video objects. What the challenge wants you to do in that method is to loop through the list of videos using the course object passed to the videosByTitle method, and for each video in this list, you add to the Map - the title of the video, and the video object itself.

After the for loop, you return the Map itself.

The Course class has a getter method that you can use to retrieve the list of video objects from the course object, and the Video class has a getter method to return the title of the video object.

Give it a shot!"

2 Answers

OK, so let's take this one step at a time :-)

What you want to return with this method is a Map. A Map has combinations of key and value, and you use the key to to find the value. Here the key is the video title (String) and the value is the video object itself (Video). The idea is that once we have this map, all we need to remember is the video title. Then we can just type:

Video javaBasicsVideo = videosMap.get("Java Basics");

This will create a reference to the Video object with the title "JavaBasics". That's very handy, and that's why we want to have this method.

So how do we do this?

  1. Create an empty map that takes a String and a Video
  2. Get the list of all course videos
  3. Loop through all the videos in the list, put each video in the map
  4. return the map

So first we create an empty map:

Map<String, Video> videosMap = new HashMap<>();

This creates an empty map. It takes a String as a key (that's the video title) and a Video as the value. But it's still empty. We need to put all the course videos inside it. We can add things to the map like that (just an example):

videosMap.put("Java Basics", javaBasicsVideo);
//javaBasicsVideo is a variable of the type Video

So we need to do something like this, but for each video in the course. How do we know which videos are in the course? Like this other person told you, there's a getter method that you can use to get all the videos in a course. Look in the Course class, it's called getVideos(). So we'll just use that:

List<Video> allVideosInTheCourse = course.getVideos();

OK, so we have all the videos in a list. Now you can use a for each loop to go through each element in that list. For each video, just put the video title and the video itself in the map you created before.

Once you're done adding all the videos to the map, after the loop, just return the map:

return videosMap;

Try putting all those steps together, and add another comment here if it's still not clear.

This is really helpful, thank you for taking the time to spell it out in baby steps. When I enter what I think I'm supposed to, its not exactly working out...

import com.example.model.Course; import com.example.model.Video;

import java.util.Map;

public class QuickFix {

public void addForgottenVideo(Course course) { // TODO(1): Create a new video called "The Beginning Bits" Video video = new Video("The Beginning Bits"); // TODO(2): Add the newly created video to the course videos as the second video. course.getVideos().add(1, video);

}

public void fixVideoTitle(Course course, String oldTitle, String newTitle) {

}

public Map<String, Video> videosByTitle(Course course) { Map<String, Video> videosMap = new HashMap<>(); videosMap.put(); List<Video> allVideosInTheCourse = course.getVideos(); for ( Video video : Course); return videosMap; }

}

Errors:

./QuickFix.java:21: error: cannot find symbol Map videosMap = new HashMap<>(); ^ symbol: class HashMap location: class QuickFix ./QuickFix.java:22: error: method put in interface Map cannot be applied to given types; videosMap.put(); ^ required: String,Video found: no arguments reason: actual and formal argument lists differ in length where K,V are type-variables: K extends Object declared in interface Map V extends Object declared in interface Map ./QuickFix.java:23: error: cannot find symbol List

Sorry for being slow...

The error you have happens because the HashMap class needs to be imported. Add this line:

import java.util.HashMap;

Then the method:

public Map<String, Video> videosByTitle(Course course) { 
Map<String, Video> videosMap = new HashMap<>(); 
videosMap.put();  //remove this line
List<Video> allVideosInTheCourse = course.getVideos(); 
for ( Video video : Course); //You're on the right track, but this line won't work. 
return videosMap; 
}

This pretty important, because for and for-each loops are everywhere! This is what a for each loop should look like (imagine nameList is a list of names):

for(String name : nameList) {
    //do something with text
}
  1. The second part of the for each loop, after the colon, has to be an iterable (for example Array, List, Set, are all iterables). So you can't just use the course there, instead you need to loop through the list you just created 'allVideosInTheCourse'.
  2. After the brackets, instead of a semicolon, you need curly braces. All the code you put inside the curly braces happens for each element in the list.

For example:

for(String name : nameList) {
    System.out.println(name);
}

This will print each name in the nameList on a separate line.

You need to do the same thing, but instead of printing the videos, you want to put them in the map. Try it again! Don't worry about being "slow". This stuff is difficult, but with more practice it will sink in, I promise.