Java实现的一个词频统计程序

时间:2014-05-22 11:29:46   收藏:0   阅读:361
import java.util.HashMap;
import java.util.Iterator;

public class WordCount {
	public static void main(String[] args) {
		String[] text=new String[]{"the weather is good ","today is good","today has good weather","good weather is good"};
		HashMap<String, Integer> hashMap=new HashMap<String, Integer>();
		for (int i=0;i<text.length;i++){
			String temp=text[i];
			String[] words=temp.split("\\s");
			for(int j=0;j<words.length;j++){
				if(!hashMap.containsKey(words[j])){
					hashMap.put(words[j], new Integer(1));
				}else{
					int k=hashMap.get(words[j]).intValue()+1;
					hashMap.put(words[j], new Integer(k));
				}
			}
		}
		Iterator iterator=hashMap.keySet().iterator();
		while(iterator.hasNext()){
			String word=(String) iterator.next();
			System.out.println(word+":"+hashMap.get(word));
		}
	}
}
海量数据可以使用MapReduce来做。

Java实现的一个词频统计程序,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!