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

olu adesina
olu adesina
23,007 Points

This task is impossible to complete as there is no method for updating keys in a hashmap or map interface

the fixVideoTitle() method requires you to update/fix the title of a the key in a hashmap but i have looked through all the documentation and there is no method for updating a key. the only way seems to remove and add a new hashset with the same value but a new key which i have done but it still says bummber. HELP!!

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.*;

public class QuickFix {

  public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
    Video vid = new Video("The Beginning Bits");

    // TODO(2):  Add the newly created video to the course videos as the second video.
           course.getVideos().add(1,vid);
  }

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {
    Map<String, Video> map = videosByTitle(course);

    Video b = map.get(oldTitle);

    map.remove(oldTitle);

    map.put(newTitle,b);


  }

  public Map<String, Video> videosByTitle(Course course) {
    Map<String, Video> v = new HashMap<>();

    for(Video vid: course.getVideos()){
         v.put(vid.getTitle(),vid);
    }

    return v;
  }

}

You are are thinking of this wrong. The task is not to replace a key in a Map, rather all you need to do is update the name of one of the videos that courses includes (A Course contains Vidoes).

This is not the most elegant solution but it will suffice for this task:

for (Video v : course.getVidoes()){
    if (oldTitle.equals(v.getTitle())){
        v.setTitle(newTitle);
    }
}

What we are doing is looping through each video in that course. If we find a video that matches our oldTitle, we simple use the setter provided to us in the Video class to set (update) that video's title.

1 Answer