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

kalyada leosrisook
PLUS
kalyada leosrisook
Courses Plus Student 17,855 Points

Can anyone help me with this task? <Java Maps>

I don't quite sure how to start in this challenge. Could anyone give me a guide to do this? thank you

com/example/BlogPost.java
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;
  }
}
com/example/Blog.java
package com.example;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

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;
  }
}

2 Answers

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hi Kalyada,

you already imported HashMap and Map. That's a good start. Next you want to create a method named "getCategoryCounts" that returns a Map<String, Integer>, String for category, Integer for the count.

In the method you first want to create the HashMap<String, Integer>, then loop over all BlogPosts in mPosts and check if the category has already been added to the map. If it is not in the map you want to put it into the map with a count of 1. If it is already in the map you want to get the count and increment it by one.

Kind Regards, Florian

kalyada leosrisook
kalyada leosrisook
Courses Plus Student 17,855 Points

Hello, thank you for the amazing explanation. I have one more problem tho, I tried to do the checking if there a category in the map but I got an error message said that "Yikes, java.lang.reflect.InvocationTargetException." what does it mean?

public Map<String, Integer> getCategoryCounts () { int count = 1; Map<String, Integer> categories = new HashMap<String,Integer>(); for (BlogPost bp : mPosts) { if (categories == null) { count = 1; } else { count++; } }

return getCategoryCounts();

}

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

I think you are getting the InvocationTargetException because you are calling the getCategoryCounts() method within itself as a return value. You want to return the categories map, not the method itself.

Then you don't want to check if categories is null but if categories.get(bP.getCategory()) is null. You want to check the category of the BlogPosts not the categories map.

So instead of "categories == null" you want to get the category by calling "String category = bP.getCategory()" then you can check inside the map with "categories.get(category) == null". If it is null you want to put it into the map by calling "categories.put(category, 1)", if it is not null you want to get the value and increment it by one "categories.put(category, categories.get(category) + 1);"

Hope this helps. The challenge is a little bit complicated to explain ;)

Kind Regards, Florian