Java Java Data Structures Exploring the Java Collection Framework Maps

Maps Code Challenge: for-each not applicable to expression type

it tells me "for-each not applicable to expression type" and it points at "for(String category : post.getCategory())" I have no clue how to solve it

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.Map;
import java.util.HashMap;

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<BlogPost, Integer> getCategoryCounts () {
    Map<String, Integer> categoriesCount = new HashMap<String, Integer>();
    for (BlogPost post : mPosts) {
        for(String category : post.getCategory()) {
        Integer count = categoriesCount.get(category);
        if (count == null) {
            count = 0;
        }
        count ++;
        categoriesCount.put(category, count);
      }
    }
    return categoriesCount;
  }
}

3 Answers

Emmanuel C
Emmanuel C
7,620 Points

Hey Dinu,

getCategory returns a String, and youre not allowed to iterate through a string, unless you call toCharArray() on it, which would allow you to iterate through each character in the string. However i dont believe thats what youre trying to accomplish.

If a BlogPost is supposed to have multiple categories, then I would suggest having a list of category in the class, and use that to iterate through them. Also List have a method called size() to get the number of items in the list.

I hope that helps, if not feel free to post some comments. Good Luck.

Steven Parker
Steven Parker
171,227 Points

You're pretty close here, I see only two issues:

The "getCategory" method returns the category as a string, just assign it to "category" directly (no loop needed).

And the method itself is defined as returning a "Map<BlogPost, Integer>", but the type of the object being returned is actually "Map<String, Integer>" — these need to match.

Fix those and you should pass the challenge!

Thanks a lot Steven and Emmanuel! I really appreciate your swift answers! I went through! I don't feel that I grasped the whole problem with the getCategory though. I'm wondering why is it in the video the method works, and in the challenge it doesn't?

Map<String, Integer> hashTagCounts = new HashMap<String, Integer>();
    for (Treet treet : treets) {
      for (String hashTag : treet.getHashTags()) {
        Integer count = hashTagCounts.get(hashTag);
        if (count == null) {
          count = 0;
        }
        count++;
        hashTagCounts.put(hashTag, count);
      }
Steven Parker
Steven Parker
171,227 Points

These examples are working with different structures. The "getHashTags" method returns an iterable collection (suitable for a loop), but "getCategory" returns a String.