import java.util.HashMap;
import java.util.Iterator;

/**
* @author Administrator
* 2011-10-20 9:25
*/
//哈希表计出现次数
public class Has {
// 统计单词出现的次数
public static String StatList(String str) {
StringBuffer sb = new StringBuffer();
HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表
String[] slist = str.split("\\b");
for (int i = 0; i < slist.length; i++) {
if (!has.containsKey(slist[i])) { // 若尚无此单词
has.put(slist[i], 1);
} else {//如果有,就在将次数加1
has.put(slist[i],has.get(slist[i])+1 );
}
}
//遍历map
Iterator iterator = has.keySet().iterator();
while(iterator.hasNext()){
String word = (String) iterator.next();
sb.append("单词:").append(word).append(" 次数").append(has.get(word)).append("\n");
}
return sb.toString();
}

public static void main(String[] args) {
String s = new String("You are the mananger of an office supplies company. A colleague has received a letter compaining about an order for office furniture. She has left the letter for you to answer and has written some notes on it.");
System.out.println(StatList(s));
}
}