I have a map of word frequencies Map<String, Integer>. I need to make a set of the least occurring words. Say the lowest occurring words all appeared twice, I need to make a set of all these twice-occurring words. So far I have:
public Set findRarest()
{
int occurrence = 1000; //high initial value for word length
for (Map.Entry<String,Integer> item : wcMap.entrySet())
{
if (item.getValue() > occurrence); //most likely for performance
else if (item.getValue() == occurrence)
{
rarest.add(item.getKey());
}
else //found new lowest count
{
rarest.clear();
rarest.add(item.getKey());
}
}
return rarest;
}
This seems a little convoluted to me. Are there native collection tools to get this done?
I don’t think your code even works as written. Two things:
Initialize
occurrencewithInteger.MAX_VALUEinstead of just some arbitrary large value.Update the value of
occurrencewhenever you find a word which occurs less often.Other than that, your solution is fine. I’m not sure you could get anything much cleaner restricting yourself to Java Collections Framework classes.
Updated code: