Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures 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,485 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!