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

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Just not getting this question

Running into a wall with this one, any help would be much appreciated. I think it might be my for statement, I'm not sure it's properly looping through the BlogPosts.

Thanks.

Here's the error message

./com/example/Blog.java:14: error: no suitable method found for addAll(String)

  allAuthors.addAll(BlogPost.getAuthor());
            ^
method Set.addAll(Collection) is not applicable

  (actual argument String cannot be converted to Collection by method invocation conversion)

method Collection.addAll(Collection) is not applicable

  (actual argument String cannot be converted to Collection by method invocation conversion)
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 static 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 static 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.TreeSet;
import java.util.HashSet;

public class Blog {
  List<BlogPost> mPosts;

public Set<String> getAllAuthors() {
    Set<String> allAuthors = new TreeSet<String>();
    for (BlogPost post : mPosts) {
      allAuthors.addAll(post.getAuthor());
    }

    return allAuthors;
 }


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

  public List<BlogPost> getPosts() {
    return mPosts;
  }
}
Christopher Augg
Christopher Augg
21,223 Points

Hello Rob,

It looks like BlogPost.getAuthor returns a String; however, the Set allAuthors.addAll() method must pass a Collection according to : http://docs.oracle.com/javase/7/docs/api/java/util/Set.html

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

It's been edited to post.getAuthor(), and I was able to find the same information when trying to figure this out, I'm still kind of lost of in what direction to take the question.

1 Answer

Hi Rob,

I think you mean to use

allAuthors.add(post.getAuthor());

in your for loop.

Or maybe you want something like this:

allAuthors.addAll(mPost.getAuthor());

without the for loop.

I'm not sure the second method will work it is just a suggestion, but since the Set.addAll() method takes a collection it would be a more proper usage. The way you are trying is adding one author at a time from mPost.

Also, I may just be talking out of my a$$. I apologize if this doesn't make any sense.

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Thank you very much. Your first one was the simplest solution so I used it, I was beyond frustrated and not thinking clearly, you're answer made sense.

Thanks again! Appreciated.

Ok cool. I wasn't sure you were set on using the addAll() method. I actually don't think it is possible in this scenario to use the second approach I suggested. Good luck with the rest!