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
eric Crawford
2,381 PointsHow does this object of type "BlogPost" work with a method of "String" type?
So i have been back tracking through this whole Java Data structures section, because im not quite grasping what is really going on.
in the "sets" objective we were asked to
: make a method called -getAllAuthors()- : have it loop through all posts :return it as a Set in alphabetical order
I solved it, but i want to know how - for (BlogPost post : mPosts) - allows the use of a String method in the following line? Isnt "post" a variable of type BlogPost Here's my actual code:
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> getAllAuthors() {
Set<String> authors = new TreeSet<String>();
for (BlogPost post : mPosts) {
authors.add(post.getAuthor());
}
return authors;
}
}
4 Answers
Kevin Faust
15,353 PointsWell let's back at the instructions. " loop over all the posts and returns a java.util.Set of all the authors, which are stored as Strings."
What you have written in the second block is wrong because look at this:
List<BlogPost> mPosts;
Our mPosts is a List right? It's a list of blogpost objects. We cannot call the getAuthor() method on a list. The getAuthor() method is a method we created in the BlogPost class. That means we can't use that method anywhere apart from the BlogPost class because it doesnt exist elsewhere.
What you need to do is first go into the list and access the BlogPost items. Now we can call the getAuthor() on each of these BlogPost items.
This method will return a string. and we add it to our set of strings.
Kevin Faust
15,353 PointsI didn't quit understand your question but I will explain what I believe you are confused with.
So we have a list of BlogPost objects under the name mPosts right? When we loop through each item in mPosts, we are grabbing a BlogPost object and storing it under the post variable (each BlogPost object we are currently looping on is called "post"). Take a look at our BlogPost class, we have a method there called getAuthor() which returns a string. So everytime we loop through a BlogPost object, we call the method and grab the string value and add it to our set. We add this to our set each loop.
I'm not sure if this is what you were looking for but let me know if you need further help,
Kevin
eric Crawford
2,381 PointsActually i wanted to know why you can return a string when the object is not a string to begin with. Maybe it sounds wierd but many times i have seen errors when trying to do this. I will try and figure out the correct wordage for what i'm trying to ask, sorry for the confusion.
Eric
Kevin Faust
15,353 PointsHey Eric,
What do you mean when we you say "i wanted to know why you can return a string when the object is not a string to begin with"? We do return a Set of strings. Is that what you meant? Or are you wondering how we are able to return a string from the getAuthor() method?
Please tell me if that's not what you meant. I think your question is how we can return a set of strings when we are dealing with BlogPost objects.
Well each BlogPost class has a getAuthor() method and this returns a string. When we loop through each BlogPost, we take the author string and add it to the set. therefore our set is made up of only strings.
eric Crawford
2,381 PointsThank you and yes that was my question. For clarity, in the 3rd line of the
getAllAuthors() method.
for (BlogPost post : mPosts) {
authors.add(post.getAuthor());
}
why couldnt I use just use
for (BlogPost post : mPosts.getAuthor()) {
authors.add(post);
}
I think i may have just realized why....is it because post is a BlogPost object and in order to call the getAuthor() method i need to store it in a object of String type?
eric Crawford
2,381 Pointseric Crawford
2,381 PointsThanks Kevin, im pretty sure i get it now, and not just being able to get it right, but understand why its right will help alot.