问题描述
我正在编写一个Java程序,用于解析文本文件中的所有单词,然后将它们添加到HashMap中。我需要计算文件中包含多少个不同的单词。我还需要计算出最高的计数单词。 HashMap由映射到一个整数的每个单词组成,它表示该单词出现的次数。
是否有像HashMap这样的东西可以帮助我排序?
您可以使用:
import com.google.common.collect。*;
import com.google.common.collect.Multiset.Entry;
...
final Multiset< String> words = HashMultiset.create();
words.addAll(...);
Ordering< Entry< String>> byIncreasingCount = new Ordering< Entry< String>>(){
@Override public int compare(Entry< String> a,Entry< String> b){
//安全,因为count永远不是负数
返回left.getCount() - right.getCount();
}
});
条目< String> maxEntry = byIncreasingCount.max(words.entrySet())
return maxEntry.getElement();
编辑:哎呀,我以为你只想要一个最常用的单词。但它听起来像你想要的几个最常见的 - 所以,你可以用 sortedCopy
替换 max
,现在你有($)
$ b
查找不同单词的数量: words.elementSet()。size()
I'm writing a Java program that parses all the words from a text file and then adds them to a HashMap. I need to count how many distinct words are contained in the file. I also need to figure out the highest counted words. The HashMap is comprised of each word mapped to an integer which represents how many times the word occurs.
Is there something like HashMap that will help me sort this?
You could use a HashMultiset from google-collections:
import com.google.common.collect.*;
import com.google.common.collect.Multiset.Entry;
...
final Multiset<String> words = HashMultiset.create();
words.addAll(...);
Ordering<Entry<String>> byIncreasingCount = new Ordering<Entry<String>>() {
@Override public int compare(Entry<String> a, Entry<String> b) {
// safe because count is never negative
return left.getCount() - right.getCount();
}
});
Entry<String> maxEntry = byIncreasingCount.max(words.entrySet())
return maxEntry.getElement();
EDIT: oops, I thought you wanted only the single most common word. But it sounds like you want the several most common -- so, you could replace max
with sortedCopy
and now you have a list of all the entries in order.
To find the number of distinct words: words.elementSet().size()
这篇关于像HashMap的东西,但排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!