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 Maps

Pascal Petruch
Pascal Petruch
1,479 Points

Map of category-to-count always returns one count too much

Hello everyone,

I dont' understand why, but I always get one count to much. If it should be 2 I get 3. There's an error in my code. Could you give me a hint?

public Map<String,Integer> getCategoryCounts() {
    Map<String,Integer> result = new HashMap<String,Integer>();
    Integer count = 0;
    for(BlogPost post : mPosts) {
      if(result.containsKey(post.getCategory()) == false) {
        count=1;
        result.put(post.getCategory(),count);
      }
      else {
        count++;
        result.put(post.getCategory(),count);
      }
    }
    return result;
  } 

1 Answer

Hi Pascal,

A week ago, I answered within this post - let me know if that sorts your issue out for you. I think that post should help you.

Steve.