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 Maps

luis martinez
luis martinez
2,480 Points

getCategoryCounts

package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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<>();
    for (BlogPost post: mPosts) {
      authors.add(post.getAuthor());
    }
    return authors;
  }

  public Map<String, Integer> getCategoryCounts()
  {
    Map<String, Integer> CategoryCounts = new HashMap<String, Integer>();
    for (BlogPost post : mPosts)
    {
      for (String Category : post.getWords())//It tells me expected 1 but returns null
      {
        Integer count = CategoryCounts.get(Category);
        if (count == null)
        {
          count = 0;
        }
        count ++;
        CategoryCounts.put(Category, count);
      }
    }
   return CategoryCounts; 
  }
}

[MOD: added ```java markdown formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

Your code is very close. There isn't a need to look at the words within each BlogPost. Only the BlogPost Category is needed.

  public Map<String, Integer> getCategoryCounts()
  {
    Map<String, Integer> CategoryCounts = new HashMap<String, Integer>();
    for (BlogPost post : mPosts)
    {
      // removed for-loop
      // for (String Category : post.getWords())//It tells me expected 1 but returns null
      // {
        Integer count = CategoryCounts.get(post.getCategory());  // <-- get current count of current post's category
        if (count == null)
        {
          count = 0;
        }
        count ++;
        CategoryCounts.put(post.getCategory(), count);  // <-- update count of current post's category
      // }
    }
   return CategoryCounts; 
  }
Grigorij Schleifer
Grigorij Schleifer
10,363 Points

Hi luis,

public Map<String, Integer> getCategoryCounts() {
    // create a Map that accepts a String as key and an Integer as value
    // so the key is the category
   // and value is the count how many categories are in the post
    Map<String, Integer> categoryCount = new HashMap<String, Integer>();

    // loop over every post in mPosts
    for(BlogPost post: mPosts) {
      String category = post.getCategory();
        // No need to create a second/nested for loop
        // create new count and proof whether a category in there using getCategory()
        // getCategory returns a null if no category is in the post
        // increment if getCategory returns not null (post is not empty)
        Integer count = categoryCount.get(category);
          if(count == null) {
            count = 0;
          }
        count++;
        // put category as key and count as value in Map
        categoryCount.put(category, count);
      }
     return categoryCount;
    }

Let me know if you need more help

Grigorij