I have a lot of words at hand. What I need to do is to save them and count every different word. The original data may contain some duplicate words.Firstly, I want to use Set, then I can guarantee that I only get the different wrods. But how can I count their times? Is there someone having any "clever" idea?
You can use MultiSet
from the Guava library.
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multiset.html
You can use Map to solve this problem.
String sample = " I have a problem here. I have a lot of words at hand. What I need to do is to save them and count every different word. The original data may contains duplicate words.Firstly, I want to use Set, then I can guarantee that I only get the different wrods. But how can I count their times? Is there someone having any clever idea?";
String[] array = sample.split("[\\s\\.,\\?]");
Map<String,Integer> statistic = new HashMap<String,Integer>();
for (String elem:array){
String trimElem = elem.trim();
Integer count = 0;
if(!"".equals(trimElem)){
if(statistic.containsKey(trimElem)){
count = statistic.get(trimElem);
}
count++;
statistic.put(trimElem,count);
}
}
也许您可以使用哈希,在Java中,它可以是HashMap(或HashSet?),您可以哈希每个单词,如果该单词已被哈希,则将与其关联的某个值加1。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.