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
isaac schwartzman
963 PointsCan translate this to english?
public Map<String, Integer> getCategoryCounts() {
Map<String, Integer> categoryCounts = new HashMap<String, Integer>();
for (BlogPost post : mPosts) {
String category = post.getCategory();
Integer count = categoryCounts.get(category);
if (count == null) {
count = 0;
}
count++;
categoryCounts.put(category, count);
}
return categoryCounts;
}
Moderator edited: Markdown added so that code renders properly on the forums.
isaac schwartzman
963 PointsOh, thanks didn't really know that was an option
Jennifer Nordell
Treehouse TeacherIt most definitely is, thank goodness! Formatting your code helps other students to get an answer out to you in a more expedient manner. And if you've got an hour to spare and want to get really fancy, I highly suggest the Markdown Basics Course
Johan Rönkkö
28,054 PointsWhat exactly do you mean by translating to english? You want an explanation of what's going on in the method?
1 Answer
Seth Kroger
56,416 PointsSuppose we have a Blog and we want to know how many posts there are in each category. We want the ability to look up a category and find the number of posts so we set of a table of a name and a number. We then go through each post in the blog, get its category, look up the category name in the table, and get the current count of posts. If we haven't encountered the category yet the entry in the table and therefore the count won't exist yet. If that happens we give it an initial count of 0. Regardless of the previous step, we always add 1 to the count. (Another way is if the count doesn't exist set it to 1, else add 1 to it) We then record the name and count back to the table.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi there, Isaac! I took the liberty of adding some markdown to your question so that it'll be easily readable on the forums. If you'd like to learn how to make your code look like this, take a look at the Markdown Cheatsheet at the bottom of the "Add an Answer" section!