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 trialKen Alger
Treehouse TeacherSet 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
Treehouse TeacherYou 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...
Zach Freitag
20,341 PointsSpoiler 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;
}
}
james white
78,399 PointsHi 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:
Roshan Kashif
2,976 PointsI get the part 1 of that challenge but a am stuck on part 2
Craig Dennis
Treehouse TeacherHey Roshan Kashif ,
Can you also add a new post. This post is a little diluted as well ;)
Stephen Wall
Courses Plus Student 27,294 PointsDon't forget to check your imports! :)
import java.util.HashSet;
mTags = new HashSet<>();
Matt Bloomer
10,608 PointsI 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?
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 PointsHi Craig, when i looked at this problem, i thought that
Either one should work, but no luck still, both got compiler error, how come?
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherWilliam;
Did you remember to add
import java.util.HashSet;
?Ken
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherI 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
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 PointsGeez, you're right, Ken, I forgot to import
java.util.HashSet
,Arthur Podkowiak
3,633 PointsArthur Podkowiak
3,633 PointsHow 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.