HashMap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mdawoud
    New Member
    • Sep 2008
    • 1

    HashMap

    Hey Guys,

    I'm hoping you could help me out here. I'm confused on how to use HashMap. I'm trying to use HashMap or HashTable to count words in a JTextArea. I have two text areas, one for editable and the other is not. So, I read a txt file into the JTextArea and I need to count how many words in that file.

    Any ideas?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by mdawoud
    Any ideas?
    Sure; if you know how to figure out how to extract words from a String, the
    following little snippet can count them for you:

    Code:
    public class WordCounter {
       private Map<String, Integer> words= new HashMap<String, Integer>();
       public void put(String word) {
          Integer n= words.get(word);
          if (n == null) // word not yet in map
             n= Integer.valueOf(0);
          map.put(word.n+1); // update count
       }
       // etc. etc.
    }
    kind regards,

    Jos

    Comment

    Working...