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 Exploring the Java Collection Framework Sets

Im lost.

Cant figure it out, it says im using "unsafe" operations when im trying to create the TreeSet. I dont even know what that means. Help would be much appreciated!

com/example/BlogPost.java
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;
  }
}
com/example/Blog.java
package com.example;

import java.util.List;
import java.util.TreeSet;

public class Blog {
  List<BlogPost> mPosts;

  public Blog(List<BlogPost> posts) {
    mPosts = posts;
  }

  public List<BlogPost> getPosts() {
    return mPosts;
  }

  public TreeSet getAllAuthors() {
    Set<String> authors = new TreeSet<String>();
    for (BlogPost post : mPosts) {
      authors.add(post);
    }
    return authors;
  }

}

1 Answer

Manish Giri
Manish Giri
16,266 Points

There are a few problems in your code.

First, look at what you're asked to return in the challenge -

Create a method in the Blog class called getAllAuthors that loops over all the posts and returns a java.util.Set of all the authors

It says returns a java.util.Set. Whereas, in your method signature you have - public TreeSet getAllAuthors() { . This returns a TreeSet, not a Set. So, you should change the signature to return Set<String> instead.

Next, look at the second half of the instruction -

and returns a java.util.Set of all the authors, which are stored as Strings

Your Set object should store all the authors which are stored as strings, meaning the names of the authors. So, you correctly declare your Set object of type String - Set<String> authors = new TreeSet<String>(); But in your for loop, you are adding a BlogPost object, not a String object -

for (BlogPost post : mPosts) {
      authors.add(post);
    }

HINT - BlogPost class has a getAuthor() method using which you can get the author name from the post object.

And lastly, as for the "uncheck" warning, I think its due to the return of TreeSet. TreeSet is a raw type, meaning you lose the type safety which comes with generics when you use the correct type, like TreeSet<String>, or TreeSet<Integer>, etc. Java compiler warns you when you try to use raw types. If you fix the issue I mentioned in Step 1, that should take care of the warning.

Hope this helps.

Hey again mate! Excellent explanation, and i think im close now. But i cant figure out you get loop thru the authors?

public Set<String> getAllAuthors() {
    Set<String> authors = new TreeSet<String>();
    for (String author : mPosts.getAuthor()) {
      authors.add(author);
    }
    return authors;
  }

But the mPosts has no .getAuthor(). So yea, thats where im stuck, how do i get the authors??

Manish Giri
Manish Giri
16,266 Points

mPosts is a List object, so it wouldn't know what getAuthor() is. It will only have access to methods defined in the List interface in java.util.List.

I was referring to your earlier for loop itself -

for (BlogPost post : mPosts) {
      authors.add(post);
    }

Here you're looping through the List of BlogPost objects, and post represents the objects in the loop. Now if you look in the BlogPost class, you have this method, which returns the name of the author as a String.

public String getAuthor() {
    return mAuthor;
  }

So, every BlogPost object has access to this method, and you can use it in your for loop to get the author's name add add it to the Set, like so-

for (BlogPost post : mPosts) {
      authors.add(post.getAuthor());
    }

Hope this helps.

Gotcha, i dont know what i was thinking, i had some idea of looping thru the .getAuthor method. It didnt make any sense tho haha. Ty so much for your patience!