Java Java Data Structures Efficiency! Changing Course

olu adesina
PRO
olu adesina
Pro Student 19,018 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;
  }

}
Connor Powers
Connor Powers
1,247 Points

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