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

Sean Flanagan
Sean Flanagan
33,235 Points

Error in Example.java Line 26

Hi.

I'm having a problem with this line. I'll paste Example.java below:

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import com.teamtreehouse.Treet;
import com.teamtreehouse.Treets;


public class Example {

  public static void main(String[] args) {
    Treet[] treets = Treets.load();
    System.out.printf("There are %d treets. %n",
                     treets.length);
    Set<String> allHashTags = new HashSet<String>();
    Set<String> allMentions = new TreeSet<String>();
    for (Treet treet : treets) {
      allHashTags.addAll(treet.getHashTags());
      allMentions.addAll(treet.getMentions());
    }
    System.out.printf("Hash tags: %s %n", allHashTags);
    System.out.printf("Mentions: %s %n", allMentions);

    Map<String, Integer> hashTagsCounts = new HashMap<String, Integer>(); //error here somewhere
    for (Treet treet : treets) {
      for (String hashTag : treet.getHashTags()) {
        Integer count = hashTagCounts.get(hashTag);
        if (count == null) {
          count = 0;
        }
        count++;
        hashTagCounts.put(hashTag, count);
      }
    }
    System.out.printf("Hash tag counts: %s %n", hashTagCounts);
  }
}

Any help here would be great. Thanks :-)

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hmmm....I don't see HashMap imported.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Craig. Thanks for your help. I've just imported HashMap as per your suggestion. I tried to run Example without the REPL but I received other error warnings. Here's Example now:

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import com.teamtreehouse.Treet;
import com.teamtreehouse.Treets;


public class Example {

  public static void main(String[] args) {
    Treet[] treets = Treets.load();
    System.out.printf("There are %d treets. %n",
                     treets.length);
    Set<String> allHashTags = new HashSet<String>();
    Set<String> allMentions = new TreeSet<String>();
    for (Treet treet : treets) {
      allHashTags.addAll(treet.getHashTags());
      allMentions.addAll(treet.getMentions());
    }
    System.out.printf("Hash tags: %s %n", allHashTags);
    System.out.printf("Mentions: %s %n", allMentions);

    Map<String, Integer> hashTagsCounts = 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);
      }
    }
    System.out.printf("Hash tag counts: %s %n", hashTagCounts);
  }
}

And the errors:

Example.java:27: error: cannot find symbol                                            
    Map<String, Integer> hashTagsCounts = new HashMap<String, Integer>();             
    ^                                                                                 
  symbol:   class Map                                                                 
  location: class Example                                                             
Example.java:30: error: cannot find symbol                                            
        Integer count = hashTagCounts.get(hashTag);                                   
                        ^                                                             
  symbol:   variable hashTagCounts                                                    
  location: class Example                                                             
Example.java:35: error: cannot find symbol                                            
        hashTagCounts.put(hashTag, count);                                            
        ^                                                                             
  symbol:   variable hashTagCounts                                                    
  location: class Example

Thanks in advance for any later help. :-)

Craig Dennis
Craig Dennis
Treehouse Teacher

Looks like Map is missing too ;)

Also check the spelling (and pluralization) of hash tag counts in your code.

Sean Flanagan
Sean Flanagan
33,235 Points

I imported Map and I've now noticed that on Line 28 I've put hashTagsCounts. I changed that to hashTagCounts. I ran the code and it's working perfectly. Thank you Craig. :-)