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

Derek Derek
Derek Derek
8,744 Points

Compile time error

Can someone please help me pass this task? Also, should I declare the return type of the method getAllAuthors as Set<String> or TreeSet<String> ? Thank you in advance!

1 Answer

Hi there,

Make sure you include the correct import statements first.

Then, create the method that returns a Set and takes no parameters. Inside, create the set to be returned; use a TreeSet for ordering. Then iterate over all posts in mPosts using a for loop adding the individual post's author to the set using add and the post's getAuthor method. Then return your Set.

It'll look like this:

  public Set<String> getAllAuthors(){
    Set<String> allAuthors = new TreeSet<String>(); // create the set
    for (BlogPost post : mPosts){    // iterate over all posts one at a time
      allAuthors.add(post.getAuthor());    // get each post's author and add to set
    return allAuthors; // return the set

I hope that helps,