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

Jacob Davis
PLUS
Jacob Davis
Courses Plus Student 2,965 Points

Java Fix Video Title Method

I may not understand completely what I am supposed to do, but I am trying to remove the old key and put in a new one with the same value of the old key. There is not a way to just change the name of the key. I feel like I am close but I might be thinking about it wrong.

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

public class QuickFix {

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

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {
   Map<String, Video> fvt = new HashMap<String, Video>();
   fvt = videosByTitle(course);
   Video video = fvt.remove(oldTitle);
   fvt.remove(oldTitle);
   fvt.put(newTitle, video);
   }

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

}

2 Answers

andren
andren
28,558 Points

While it's true that you can't change a key that is not actually what the task is asking for. It is asking you to change the title property of the video, not to change the videosByTitle map.

The videosByTitle map is useful for figuring out what video to modify, but once you have found said video you don't need to mess with that map anymore. In the same way you can pull out the title of a video by using getTitle you can set the title of a video object by using setTitle.

I feel that is enough of a hint for this task.

Jacob Davis
PLUS
Jacob Davis
Courses Plus Student 2,965 Points

Thanks, that got it to work. I figured I was't thinking about it correctly.