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

How do I see if a tag was added to mTags set?

Hi,

I'm on the last part of this challenge, but I really can't figure out how do to what the instructions say. I'm supposed to see if a tag has been added to the mTags method through using hasTag. Here is my code. Hope you can help.

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

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

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

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

  public void addTag(String tag) {
    mTags = new TreeSet<>();
    mTags.add(tag);
    // TODO: add the tag
  }

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

  public boolean hasTag(String tag) {
    // TODO: Return whether or not the tag has been added
    mTags.add(tag);
    return true;

  }

  public String getTitle() {
    return mTitle;
  }

}

1 Answer

I noticed you're a bit off with the logic regarding task 2. When you add a tile, you shouldn't reset the current tags. So here're all three methods needed in order to pass tasks 2 and 3:

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

public void addTags(List<String> tags) {
  mTags.addAll(tags);
}

public boolean hasTag(String tag) {
  return mTags.contains(tag);
}