Java Java Data Structures Efficiency! Add tags to a course

Chris Gauthier
PRO
Chris Gauthier
Pro Student 6,434 Points

Data stucture challenge.

Hello, here is the next challenge. Can somebody explain to me clearly what they're looking for here? I assume that this is a good solution, but it doesn't pass the challenge.

Can somebody help?

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

import java.util.List;
import java.util.Set;
import java.util.HashSet;

public class Course {
  private String mTitle;
  private Set<String> mTags;

  public Course(String title) {
    mTitle = title;
    // TODO: initialize the set mTags
   mTags = new HashSet<String>();
  }

  public void addTag(String tag) {
    // TODO add the tag
    mTags.put(tag);
  }

  public void addTags(List<String> tags) {
    // TODO: add all the tags passed in
    for (List<String> tag : Mtags) {
        mTags.put(tag); 
    }
  }

  public boolean hasTag(String tag) {
    // TODO: Return whether or not the tag has been added
    return false;
  }

  public String getTitle() {
    return mTitle;
  }

}

2 Answers

Yanuar Prakoso
Yanuar Prakoso
15,186 Points

Hi Chris

This is where your problems lies in your code:

public void addTag(String tag) {
    // TODO add the tag
    mTags.put(tag);
  }

  public void addTags(List<String> tags) {
    // TODO: add all the tags passed in
    for (List<String> tag : Mtags) {
        mTags.put(tag); 
    }

First of all Set does not have method call put. If you want to add one component in this case string you need to use add method!

Second the your addTags method is wrong. You need to iterate all Strings in List called tags not Mtags and fetch it as a String (not List<String>) called tag. Then once again you add it (not put) into the mTags Set.

Your code should be like this:

public void addTag(String tag) {
    // TODO add the tag
    mTags.add(tag);
  }

  public void addTags(List<String> tags) {
    // TODO: add all the tags passed in
    for (String tag : tags) {
        mTags.add(tag); 
    }

I hope this can help a little. happy coding!