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

Simon Andersson
Simon Andersson
6,880 Points

Initializing a new Set in the constructor?

I don't really get why this isn't working.

Since the Set is already declared as a member varible

private Set<String> mTags;

Shouldn't it be enough to just initialize it like I've done in the constructor?

mTags = new HashSet<String>();

Or am I missing something here? It gives me an error saying "Cannot find Symbol HashSet" with a marker pointing at HashSet

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

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

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
  }

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

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

  public String getTitle() {
    return mTitle;
  }

}

1 Answer

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

I believe you need to import the hashset:

import java.util.Hashset;

Simon Andersson
Simon Andersson
6,880 Points

I tried that but it says

./com/example/model/Course.java:5: error: cannot find symbol import java.util.Hashset; ^

symbol: class Hashset location: package java.util ./com/example/model/Course.java:14: error: cannot find symbol mTags = new HashSet(); ^

Simon Andersson
Simon Andersson
6,880 Points

Nevermind, it worked. I just wrote import "Hashset" instead of "HashSet" this time.

Thanks!