Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HashMap to count instances

I have the following code to count the instances of different strings in an array;

String words[] = {"the","cat","in","the","hat"};
HashMap<String,Integer> wordCounts = new HashMap<String,Integer>(50,10);
for(String w : words) {
    Integer i = wordCounts.get(w);
    if(i == null) wordCounts.put(w, 1);
    else wordCounts.put(w, i + 1);
}

Is this a correct way of doing it? It seems a bit long-winded for a simple task. The HashMap result is useful to me because I will be indexing it by the string.

I am worried that the line

else wordCounts.put(w, i + 1);

could be inserting a second key-value pair due to the fact that

new Integer(i).equals(new Integer(i + 1));

would be false, so two Integers would end up under the same String key bucket, right? Or have I just over-thought myself into a corner?

like image 332
lynks Avatar asked Oct 31 '12 17:10

lynks


1 Answers

Your code will work - but it would be simpler to use HashMultiset from Guava.

// Note: prefer the below over "String words[]"
String[] words = {"the","cat","in","the","hat"};
Multiset<String> set = HashMultiset.create(Arrays.asList(words));

// Write out the counts...
for (Multiset.Entry<String> entry : set.entrySet()) {
    System.out.println(entry.getElement() + ": " + entry.getCount());
}
like image 172
Jon Skeet Avatar answered Nov 15 '22 00:11

Jon Skeet