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

Exploring the Java Collection Framework - Sets Challenge

I have a feeling that I'm close (please correct me if I am wrong), but I don't know what to put for the values that, in the video, Craig Dennis put for:

for (Treet treet : treets) 

What are these values, and how do they work in the "formula" of the way the code is set up?

My code for the challenge is below; I added comments where the values in question are supposed to go (where it says "Something").

Any assistance and further explanation of how this all works would be greatly appreciated. :)

Thanks!

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.HashSet;     // Added this...
import java.util.Set;         //...this...
import java.util.TreeSet;     //...and this...

public class Blog {
  List<BlogPost> mPosts;

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

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

  //Then the rest is here
  Set<String> getAllAuthors = new TreeSet<String>();
  for (BlogPost /* Something */ : /*Something */) {
    getAllAuthors.addAll(/* Something */.getAuthor());
  }

}

1 Answer

Allan Clark
Allan Clark
10,810 Points
//Then the rest is here
  Set<String> getAllAuthors = new TreeSet<String>();
  for (BlogPost /* Something */ : /*Something */) {
    getAllAuthors.addAll(/* Something */.getAuthor());
  }

First we are missing the method declaration here. It should be public and return a Set of Strings. Then we have a for-each loop. The first "something" is just a variable name for the specific BlogPost we will be using on each cycle of the loop. The second "something" is the collection that the for each loop is looping through. So far we should have something like this:

public Set<String> getAllAuthors() {
   Set<String> allAuthors = new TreeSet<String>();
   for(BlogPost post : mPosts){ 
      // code to get the author and add it to the Set
   }
   return allAuthors;
}

Now we just need the code for inside the for-each loop. We are using the variable 'post' as the container for the element selected in each loop cycle. So we should just need to get the author of 'post' and add it to the Set. Of course returning the set at the end.

for(BlogPost post : mPosts){ 
      allAuthors.add(post.getAuthor());
   }

Thanks! This code got it:

package com.example;

import java.util.List;
import java.util.HashSet;     // Added this...
import java.util.Set;         //...this...
import java.util.TreeSet;     //...and this...

public class Blog {
  List<BlogPost> mPosts;

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

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

  //And here's the rest...
  public Set<String> getAllAuthors() {
   Set<String> allAuthors = new TreeSet<String>(); 
    for (BlogPost post : mPosts) {
      allAuthors.add(post.getAuthor());
    }
    return allAuthors;
  }

}

So, if I understand correctly, for one, I hadn't defined it (maybe I thought I had defined it on the line where I made the "new" one, where I had "Set<String> getAllAuthors = new TreeSet..." in the original code?), for two, I wasn't returning anything (should have caught that), and for three, that "post" (in the for loop) is a variable/key/something that we define right there, in the method itself? (So for that "something", it doesn't necessarily matter what you call it? )

Thanks!

Or, on that for-loop, is it:

for (BlogPost post : mPost)

/* mPost is a value of the key "post" which is of type BlogPost
but "post" is just defined right here, and just takes the value of mPost */

Something along those lines? :)

Thanks!