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

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Set initialization

I am obviously missing something with Task 1 of this challenge, which states:

Let's add a new feature that allows tags to be added to courses. Each course can only be tagged once with a specific tag, no duplicates. Sounds like a Set to me...

I've added the private set mTags. For the first task, can you please initialize it in the Constructor.

We are given the prompting code for the Constructor with mTags already declared thusly:

  private String mTitle;
  private Set<String> mTags;

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

I am absolutely baffled as to what I am missing here, some of the various ways I have attempted this are:

Set mTags = new HashSet<>();

No syntax errors on that, I am just reminded that I need to initialize mTags. Yes, I added the java.util.HashSet to the imports. That seemed logical after working through the videos.

Set<String> mTags = new HashSet<>();

&

Set<String> mTags = new HashSet<String>();

Same thing.

I even tried to instantiate just Set, which being abstract shouldn't be able to be done. Thankfully that didn't work either.

What am I missing?

Thanks for the help.

Ken

5 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

You are creating a new local variable in that scope by redeclaring the variable. Sorry I will add a new check about that.

Just start the line with mTags...

William Li
William Li
Courses Plus Student 26,868 Points

Hi Craig, when i looked at this problem, i thought that

mTags = new HashSet<String>();

// or
mTags = new HashSet<>();

Either one should work, but no luck still, both got compiler error, how come?

Ken Alger
Ken Alger
Treehouse Teacher

William;

Did you remember to add import java.util.HashSet;?

Ken

Ken Alger
Ken Alger
Treehouse Teacher

I thought I tried that as well:

mTags = new HashSet<String>();

Apparently doing Java late at night requires java, which I don't drink. Thanks for the tip Craig Dennis.

Ken

William Li
William Li
Courses Plus Student 26,868 Points

Geez, you're right, Ken, I forgot to import java.util.HashSet, :disappointed_relieved:

Arthur Podkowiak
Arthur Podkowiak
3,633 Points

How come we have to make a new "HashSet", why can't we just make a new "Set". I'm having trouble understanding why we keep using HashSet and HashMap.

Spoiler Alert!

package com.example.model;

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

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

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

  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;
  }

}

Hi to all,

Thanks for the help getting past Part 1 of the challenge:

https://teamtreehouse.com/forum/java-data-structures-efficiency-add-tags-to-course

I've started another thread for those (like me) stuck on part 2:

https://teamtreehouse.com/forum/java-data-structures-efficiency-add-tags-to-course

Just wanted to mention that because it's not coming up linked to the forum search for this challenge:

https://teamtreehouse.com/forum/code-challenge:7822

I get the part 1 of that challenge but a am stuck on part 2

Craig Dennis
Craig Dennis
Treehouse Teacher

Hey Roshan Kashif ,

Can you also add a new post. This post is a little diluted as well ;)

Stephen Wall
PLUS
Stephen Wall
Courses Plus Student 27,294 Points

Don't forget to check your imports! :)

import java.util.HashSet; 
mTags = new HashSet<>(); 

I am going to go back and watch the other videos because I am a little lost, but can someone tell me the difference between a Set and a HashSet?