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

Okay its official I need help. I literally tried everything please someone help!!!

I have tried Name the interface as a BlogPost to loop over the post and as a string interface as instructed which makes no sense and i'm still not getting anywhere.

3 Answers

deckey
deckey
14,630 Points

Hi, show us what you have tried, paste some code here so everyone can see and help out. It's pretty straightforward but you need to read the assignment a couple of times good luck!

Hi Deckey,

I am sorry about that I never knew my code never got paste, its been the second time since this happen to me, here it is.

package com.example;

import java.util.List; import java.util.Arrays; import java.util.ArrayList;

public class Blog { List<BlogPost> mPosts;

public SortedSet<String> getAllAuthors() { SortedSet<String> myPost = new TreeSet<String>(getPosts()); return myPost; }

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

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

deckey
deckey
14,630 Points

ok, getAllAuthors() method is a start, but not quite there yet.

public SortedSet getAllAuthors() {
    SortedSet myPost = new TreeSet(getPosts());
    return myPost; }

Challenge asks for:

'Create a method in the Blog class called getAllAuthors that loops over all the posts and returns a java.util.Set of all the authors, which are stored as Strings. They should be sorted alphabetically.'

So, first off, your method should return a Set (of Strings), so declared like

public Set<String> getAllAuthors()

By reading through the assignment, you see that output should be sorted, so your line:

SortedSet myPost = new TreeSet(getPosts());

should be a Set instead of SortedSet, and it should be empty, no need to call 'getPosts()'. Reason for that is that you don't want posts in the set, you need authors of those posts. Also, getPosts returns list of type BlogPost, not String.

Set <String> postSet = new TreeSet<>();

As a last step, you'll need to loop through BlogPost elements of mPosts and 'add' each element's author to this new postSet. Try it and post your progress here

good luck!

Hello Dickey,

This is my current progress:

package com.example;

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 Set<String> getAllTheAuthors() { Set<String> myAuthors = new TreeSet<>(); for(String postInAuthors : mPost) { myAuthors.addAll(postInAuthors); } return myAuthors; }

Hi Chevano,

Did you finally manage to get past this challenge?