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 trialZack Klinger
17,622 PointsI can't figure out from the Treet.java and Example.java and compiler errors what I'm expected to do here...
We wrote out Map<String, Integer> and then again with Map<String, List<Treet>>. Neither one are helping me to understand this last exercise.
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;
}
}
package com.example;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
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> categories = new HashMap<String, Integer>();
for (BlogPost post : mPosts) {
Integer count = categoryCounts.get(post);
if (count == null) {
count = 0;
}
++count;
categories.put(category, count);
}
return categories;
}
}
3 Answers
Michael C
3,230 PointsCan you post your compiler errors at least? That'll help a lot to figure it out!
Christopher Augg
21,223 PointsHello Zack,
You are very close! I think we can get you on track quickly as you have the right idea and only a few bugs in your code.
1) you named the Map categories but then you try to pass the key into a Map called categoryCounts to acquire the Integer count. 2) you pass in the object called post when you need to do post.getCategory() so it will return the String for it and then the map get method can return the Integer. 3) When using the put method, you pass in put(category, count). However, you need to use the post.getCategory() method again in order to acquire the String for category.
I can provide you a code example if you want, but please try and use the above information to correct your minor bugs.
Zack Klinger
17,622 PointsYeah, that was in between three different versions I tried. I looked at the other Q&A's and figured out what I wasn't getting. I still need to re-watch the whole section from beginning to end before I move on WITHOUT typing along...
Thanks for the help. Treehouse is 2x'ing my college classes like crazy! I can't believe how fast all this info is coming, but it's fun...
Christopher Augg
21,223 PointsYea, I know the feeling. I just recently graduated from the University of Washington with a major in Computer Science. CS classes are much more demanding. Especially if you are near the end doing OS, Data Structures, Algorithms, compiler theory, Artificial Intelligence, Formal Models, etc. It is all a lot of fun though! Keep up the good work man!