给定字符串数组,用map的key保存数组中字符串元素,value保存字符串元素出现次数,最后统计个字符串元素出现次数

import java.util.HashMap;


public class map1 {
	public static void main(String[] args) {
		String[] array = {"a","b","a","b","c","a","b","c","b"};
		HashMap<String, Integer> hm = new HashMap<>();
		for (String str : array) {
			if(hm.containsKey(str))
			{
				hm.put(str, hm.get(str)+1);
			}
			else
				hm.put(str, 1);
		}
		System.out.println(hm);
	}
}
原文地址:https://www.cnblogs.com/masterlibin/p/4774296.html