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 Exploring the Java Collection Framework Maps

Brian Maimone
PLUS
Brian Maimone
Courses Plus Student 1,644 Points

Having trouble with challenge.

Goal is to add a new method called getCategoryCounts. It should return a Map of category to count calculated by looping over all the posts. My error is Bummer! For category Entertainment, expected 1 but received null. I think the problem may be in the nested for each loops but I don't know how to debug/test on this platform. Thanks.

My code:

public class Blog { List<BlogPost> mPosts;

public String Key; public int count;

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; } Set <String> categories = new HashSet<String>(); //Get unique set of categories public Set<String> getAllCategories() {

 for (BlogPost post: mPosts) {
  categories.add(post.getCategory());
    }
   return categories;  
   }
  public Map<String, Integer> getCategoryCounts() {
 // Converting categories Set to Array
String[] arrayString = categories.toArray(new String[categories.size()]);
Map<String, Integer> categoryCounts = new HashMap<String, Integer>();
for (String Key : arrayString) {
      count = 0;
    for (BlogPost post: mPosts) {
      if (Key == post.getCategory())
        count++;
        categoryCounts.put(Key,count);
      }
    }    
     return categoryCounts;
  }

}

package com.example;

import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.List;

public class BlogPost implements Comparable<BlogPost>, Serializable { private String mAuthor; private String mTitle; private String mBody; private String mCategory; private Date mCreationDate;

public BlogPost(String author, String title, String body, String category, Date creationDate) { mAuthor = author; mTitle = title; mBody = body; mCategory = category; mCreationDate = creationDate; }

public int compareTo(BlogPost other) { if (equals(other)) { return 0; } return mCreationDate.compareTo(other.mCreationDate); }

public String[] getWords() { return mBody.split("\s+"); }

public List<String> getExternalLinks() { List<String> links = new ArrayList<String>(); for (String word : getWords()) { if (word.startsWith("http")) { links.add(word); } } return links; }

public String getAuthor() { return mAuthor; }

public String getTitle() { return mTitle; }

public String getBody() { return mBody; }

public String getCategory() { return mCategory; }

public Date getCreationDate() { return mCreationDate; } }

2 Answers

Iain Diamond
Iain Diamond
29,379 Points

Hi Brian, I've tidied up the code so it's easier to read.

//Get unique set of categories 
public Set getAllCategories() {
   for (BlogPost post: mPosts) {
     categories.add(post.getCategory());
   }
   return categories;  
 }

public Map<String, Integer> getCategoryCounts() {
  // Converting categories Set to Array
  String[] arrayString = categories.toArray(new String[categories.size()]);
  Map<String, Integer> categoryCounts = new HashMap<String, Integer>();
  for (String Key : arrayString) {
    count = 0;
    for (BlogPost post: mPosts) {
      if (Key == post.getCategory())
        count++;
        categoryCounts.put(Key,count);
    }
  }    
  return categoryCounts;
  }
}

Essentially you want to create a map to store the categories and count values. Loop over each post. Use Craig's method of getting the count from the map. Update your count value carefully and store the category and count value in the map as you go. Finally after all the looping is complete return your map.

Hope this helps. iain

Kyle Salisbury
seal-mask
.a{fill-rule:evenodd;}techdegree
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 Points

I'm still having trouble on this. When I use the code above it says there are like 8 errors, all telling me that categories is an unknown symbol. Not sure what I'm doing.

Kyle Salisbury
seal-mask
.a{fill-rule:evenodd;}techdegree
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 Points

I see in the top method I need Set<String> categories = new HashSet<String>; but then I still get 3 more errors now with the code line String[] arrayString = categories.toArray(new String[categories.size()]);