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 trialParamvir Singh
1,517 PointsTreeSet was not found even though the java.util.Set was imported
I imported all the required packages and correctly initialized the Set interface but still, it is giving me the error.
package com.example;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class BlogPost implements Comparable<BlogPost>, Serializable {
private String mAuthor;
private String mTitle;
private String mBody;
private String mCategory;
private Date mCreationDate;
public BlogPost(String author, String title, String body, String category, Date creationDate) {
mAuthor = author;
mTitle = title;
mBody = body;
mCategory = category;
mCreationDate = creationDate;
}
public int compareTo(BlogPost other) {
if (equals(other)) {
return 0;
}
return mCreationDate.compareTo(other.mCreationDate);
}
public String[] getWords() {
return mBody.split("\\s+");
}
public List<String> getExternalLinks() {
List<String> links = new ArrayList<String>();
for (String word : getWords()) {
if (word.startsWith("http")) {
links.add(word);
}
}
return links;
}
public String getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
public String getBody() {
return mBody;
}
public String getCategory() {
return mCategory;
}
public Date getCreationDate() {
return mCreationDate;
}
}
package com.example;
import java.util.List;
import java.util.Set;
import com.example.BlogPost;
public class Blog {
List<BlogPost> mPosts;
public Blog(List<BlogPost> posts) {
mPosts = posts;
}
public Set<String> getAllAuthors () {
Set<String> setOfAuthor = new TreeSet<String>();
for ( BlogPost x : mPosts ) {
setOfAuthor.add(x.getAuthor);
}
return setOfAuthor;
}
public List<BlogPost> getPosts() {
return mPosts;
}
}
2 Answers
Yanuar Prakoso
15,196 PointsHi There
You need to revise two things:
- Add import java.util.TreeSet;
- in the x.getAuthor you need to add () since you are calling a method from other class it should be: x.getAuthor()
here is the code should looks like:
package com.example;
import java.util.List;
import java.util.Set;
//import com.example.BlogPost;
import java.util.TreeSet;//<--you need to import this
public class Blog {
List<BlogPost> mPosts;
public Blog(List<BlogPost> posts) {
mPosts = posts;
}
public List<BlogPost> getPosts() {
return mPosts;
}
public Set<String> getAllAuthors () {
Set<String> setOfAuthor = new TreeSet<String>();
for ( BlogPost x : mPosts ) {
setOfAuthor.add(x.getAuthor());//<--you need to add () on the getAuthor method calling
}
return setOfAuthor;
}
}
PS Set and TreeSet is two different objects TreeSet is a class and Set is an interface. Although TreeSet as a Class implements Interface Set you still needs to import them in separate import statements.
I hope this can help a little. Happy coding
Yanuar Prakoso
15,196 PointsHi Paramvir
The simplest and easiest answer I can give you is because there are many class that implement Interface Set. Thus you must be really clear on which class you want to call to act as a wrapper for the Interface Set you call to instantiate Set<String> setOfAuthor. in your code.
Basically, you cannot use an interface directly since interface only contains fields and methods signatures (name, parameters, exceptions). To gain full implementation of an interface you need to wrap it into a class that implements that interface. Therefore you wrap your Set<String> setOfAuthor into new TreeSet<String>(). Since you call two objects one is interface Set and the other is a class TreeSet you need to import both packages.
If you want to know more about interface and class you can visit this link and this link from Oracle Java documentation. Moreover, you can google to find out more.
I hope this can help a little
Paramvir Singh
1,517 PointsParamvir Singh
1,517 PointsHi Yanuar,
With your help, I was able to solve the question.
TreeSet is a class which implements Set interface so why I just cannot write java.util.Set; to use TreeSet implementation. ?
Thanks a lot!!