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

Kenzo Qui
Kenzo Qui
2,350 Points

How do I make a "for list/array loop"

I have no idea what to put there in the array in Blog

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

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> allAuthors = new HashSet<String>();
    for(BlogPost authors : whatdoidohere) {
      allAuthors.addAll(authors.getAuthor());
    }
    return allAuthors;
  }
}

1 Answer

You need to loop through all the blog posts. For each post, you add the author to the set. You're almost there :-) In your Blog class, you have a list of blog posts stored in the variable mPosts. That's what you should use in your loop.

Also you should use a TreeSet rather than a HashSet, because you want the authors to be sorted alphabetically. The TreeSet will keep the author names sorted.

  public Set<String> getAllAuthors() {
    Set<String> allAuthors = new TreeSet<String>();
    for (BlogPost post : mPosts) {
      allAuthors.add(post.getAuthor());
    }
    return allAuthors;
  }
Kenzo Qui
Kenzo Qui
2,350 Points

Thank you very much for explaining this to me, sometimes I forget or just don't get it and might probably might be Craig not explaining much. I'm not shifting faults but sometimes I want information to be given immediately or I cannot come across the point, example, lesson he is trying to teach. That is just my expression but I am sorry I'm turning a thank you into a paragraph rant of how stupid I am but thank you very much.