public class FindMaxWord {
// 统计单词出现的次数
public static String StatList(String str) {
StringBuffer sb = new StringBuffer();
HashMap<String, Integer> has = new HashMap<String, Integer>(); // 打开一个哈希表
String[] slist = str.split("\\s");
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();
int max=0;
String maxWord = "";
while (iterator.hasNext()) {
String word = (String) iterator.next();
if(has.get(word).intValue() > max){
max = has.get(word);
maxWord = word;
}
}
return maxWord;
}
public static void main(String[] args) {
String s = new String(
"hello hell hello you are you you");
System.out.println(StatList(s));
}
}
// 统计单词出现的次数
public static String StatList(String str) {
StringBuffer sb = new StringBuffer();
HashMap<String, Integer> has = new HashMap<String, Integer>(); // 打开一个哈希表
String[] slist = str.split("\\s");
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();
int max=0;
String maxWord = "";
while (iterator.hasNext()) {
String word = (String) iterator.next();
if(has.get(word).intValue() > max){
max = has.get(word);
maxWord = word;
}
}
return maxWord;
}
public static void main(String[] args) {
String s = new String(
"hello hell hello you are you you");
System.out.println(StatList(s));
}
}