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

Steven Morimoto
PLUS
Steven Morimoto
Courses Plus Student 1,836 Points

I can't figure out the Sets quiz

Hi,

I've been working on this one question for the past 2 and a half days. I'm really at a loss as to how to solve this problem and I would greatly appreciate the assistance. I suspect there is something wrong with the above lists being assigned the generic BlogPost which makes it incompatible with a String, but I've been stuck on trying to work around that problem with all the leads that I've come up with being exhausted. To put it in a single phrase, how do I solve this?

Thanks in advance,

Steve

PS. Why does the compiler keep saying that my for statement requires a ; at the end when I know that it does not? What exactly is the compiler saying is wrong?

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.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Blog {
  List<BlogPost> mPosts;

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

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

  public List<String> getAllAuthors()
  {
    List<String> names = new ArrayList<String>();
    for(String authorNames = getPosts())
    {
      names.add(authorNames);
    }
    Set<String> set = new TreeSet<String>(names);
    return names;
  }
}

1 Answer

package com.example;
import com.example.BlogPost;                           //dont forget your import!!! its important!!! :D

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

public class Blog {
  List<BlogPost> mPosts;
  TreeSet<String> authors = new TreeSet<>();

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

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

  public Set<String> getAllAuthors() { //the requested method
    for (BlogPost post : mPosts) {          //the loop (for each post in the list mPosts...
    authors.add(post.getAuthor());        //add that posts other to my list of String Authors :D
    }
    return authors;                                   //return my alphabitised none duplicate list 

  }
}