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
PRO
Cindy Lea
Pro Student 6,482 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!