import java.util.HashMap;
public class Test{
/**
* 功能:计算数组中任意一个单词出现的次数
* @param args
*/
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
String[] source = {"f","a","b", "a", "a", "b","h"};
String temp = "b";
putMap(map, source, temp);
if (map.get(temp) == null) {
System.out.println("这个单词没有出现过");
} else {
System.out.println(map.get(temp));
}
}
private static void putMap(HashMap<String, Integer> map, String[] source, String temp) {
for (String e: source) {
if (map.isEmpty()) {
if (e == temp) {
map.put(e, 1);
} else {
map.put(e, 0);
}
} else {
if (map.containsKey(temp)) {
if (e == temp) {
map.put(e, map.get(e) + 1);
} else {
map.put(e, map.get(e));
}
} else {
if (e == temp){
map.put(e, 1);
} else {
map.put(e, map.get(e));
}
}
}
}
}
}