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

qifeng sun
qifeng sun
6,274 Points

Hi guys need help with task 2. Have no idea how to do this

Can get my head wrap around this

The question is Now I'd like to be able to get to the videos by their names, can you use the videosByTitle method to generate the proper map of title to video. We'll use it in the next step.

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;
  }

  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;
import java.util.List;

public class QuickFix {

  public void addForgottenVideo(Course course) {

    // TODO(1):  Create a new video called "The Beginning Bits"
    List<Video> videoList = course.getVideos();

    Video video = new Video("The Beginning Bits");

    videoList.add(1, video);



    // TODO(2):  Add to the course videos as the second video.
  }

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

  }

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

}

2 Answers

You are being passed a Course as a parameter in the videosByTitle method. Given what you learned about Maps and Lists, take a look at the Course class. One of the properties is a list of videos and you are given a way to access that list. Same goes for the Video class, you can access the title of each video. Your goal is to create a Map of video titles mapped to their respective videos. Hint: You can loop over the list of videos and add the title and video to your Map. Hope that helps!

Somebody was supposed to post the answer. I wanted to know the solution as well and no Rafal, you explanation does not help me much.