Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Exploring the Java Collection Framework Sets

Don't know what to do.

2 Answers

Allan Clark
Allan Clark
10,810 Points
Set<String> links = new TreeSet<String>();
for (String word : links) {

This creates a new empty TreeSet and then starts a for each loop on that set. Because it is an empty Set the for each is skipped and the Set is returned. What you want to do here is create that Set to collect all the authors in the BlogPosts. Then go through each BlogPost and add the author to the Set.

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

And of course return your authors Set.

Allan Clark
Allan Clark
10,810 Points

Saw what you posted on the other forum, gonna respond on here for moar points MUAHAHAHA :D Also not to confuse conversations.

Try adding the generic version of set to the variable declaration.

Set allAuthors = new TreeSet<String>(); 

change that Set to Set<String>

Here's my Blog class:

package com.example;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.ArrayList;

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> links = new TreeSet<String>();
    for (String word : links) {
      if (word.startsWith("@")) {
        links.add(word);
      }
    }
    return links;
  }
}

It says:

:heavy_multiplication_x: Bummer! Expected 5 authors, but received 0