简体   繁体   中英

how to count frequency of a word present in hashtable using java

I have a small assignment, where I have a in Hashtable format. Now I want to find a number of times a word is present in that.

Kindly guide me in this. thanks Edit#1

Hashtable<String, Integer> h = new Hashtable<String, Integer>();

Edit #2

if(spam.containsKey(s)){
                                  int value = spam.get(s);
                                  value += 1;
                                  spam.put(s,value);
                              }else{
                                  spam.put(s,1);
                              }

Ok, I changed my code to this now. I will have the count of that word as a value.

You will always have 0 or 1 occurrences of a specific word since a Hashtable does not allow key duplicates.

If you do h.add("hi",1) and then h.add("hi",2) and then you do n = h.get("hi") you will get 2. And h will contain just one "hi" string as key.

A common assignment is to use a hash-table like the one you show for a slightly different problem: to find the word-frequencies in a section of text (typically given as a String). Are you perhaps confused by the wording of the assignment?

If my hypothesis is correct, then here is a small hint: you have to fill the hash table in such a way that the hash maps words (the keys in the hash table) to the frequence with which they occur in the text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM