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

I am obviously doing something wrong...

I really need help, and all this stuff is not sticking in my head... What should I do?

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.Set;
import java.util.HashSet;

public class Blog {
  List<BlogPost> mPosts;

  public getAllAuthors() {
    Set<String> allPosts = new HashSet<String>();
    for (BlogPost Blog : getPosts) {
      AllPosts.addAll(Blog.getPosts());
    }

  }

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

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

1 Answer

Simon Coates
Simon Coates
28,693 Points

here's some hints:

  public /* Add return type here*/ getAllAuthors() {
    Set<String> allPosts = new HashSet<String>(); //to get ordering use TreeSet, not HashSet
    for (BlogPost blog : getPosts()) { //I added () as you're calling method not using mPosts direct. //Also changed B to b
      allPosts.addAll(//1)I changed A to a //maybe add, not addAll?
        blog.getPosts() // The method here is wrong. look at the blog class to find a method returning author
      ); 
    }
    //put return statement here
  }

remember import statements Set and the Set implementation (HashSet orTreeSet).

update: 1) the altered case stuff doesn't really matter, but it's best practice to use camel case for normal variables (excluding constants). 2) methods invocations require () and variables don't. if you'd used mPosts, the () would be unnecessary. 3) Excluding object constructors, method definitions require a specified return type. It has to be the type returned, or void to indicate no value is returned. Here, we do want to return a value. This also requires the return statement at the end of the method.

Simon Coates
Simon Coates
28,693 Points

If you just want to see the correct method code, here's a link: https://teamtreehouse.com/community/its-shows-bummer . You'll still need to change an import statement or two.