Reputation: 2760
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.
Upvotes: 0
Views: 3442
Reputation: 3858
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.
Upvotes: 1
Reputation: 10117
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.
Upvotes: 4