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! Add tags to a course

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,196 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!

yep, that got me there, thanks a bunch!