我有地图的单词频率Map<String, Integer>
的。我需要制作一组最少出现的单词。假设发生的最低的单词都出现了两次,我需要制作一组所有这些两次出现的单词。到目前为止,我有:移调键与地图的最低值到一组
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;
}
这似乎有点令我费解。是否有本地收集工具来完成这项工作?
不是真的。番石榴的'Multiset'可能会使它不那么笨拙,但这基本上就是做这件事的方法 - 除了没有任何理由拥有'if(item.getValue()>出现)'行外。 – 2012-04-22 17:57:10