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

Stuck on Sets Challenge Java Data Structures

I cannot figure out what is wrong with my code. Has been frustrating me for the past couple of days. The challenge is as follows:

I've added a new class called Blog. It is initialized with a list of blog posts. Create a method in the Blog class called getAllAuthors that loops all the posts and returns a java.util.Set of all the authors. They should be sorted alphabetically. My code is as follows:

package com.example;

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

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> authors = new TreeSet();
    for (BlogPost posts:mPosts){
      authors.add(posts.getAuthor());
    }
    return authors;
  }
}

2 Answers

Allan Clark
Allan Clark
10,810 Points

Don't forget to import your TreeSet.

I wish I could give you an excuse for this silly mistake by saying that it is a Monday, but sadly, it's Tuesday... :/ aha

Allan Clark
Allan Clark
10,810 Points

haha it happens to the best of us :D

May the Java force be with you