Java Java Data Structures Efficiency! Add tags to a course

Enyang Mercy
PRO
Enyang Mercy
Pro Student 2,339 Points

Designing UI if tag is added to mTags

Please check the if statement Can't compile again

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

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

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

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

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

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

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

  public String getTitle() {
    return mTitle;
  }

}

1 Answer

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

Hi there,

The hasTag method receives a tag and returns true if mTags contains tag, or false otherwise.

The List class has a helpful method called .contains() which you can chain to the list variable, mTags and pass in the tag that the method receives.

This will create a true or false outcome so you can just return the result of that expression from the method.

So, take mTags, chain .contains() onto it with dot notation and pass tag into contains() inside the brackets. Return that.

Steve.