public class MapDemo {
/*接收String类型的参数*/
public static Map<Character, Integer> count(String str){
Map<Character,Integer> map = new HashMap<Character,Integer>();
char[] arr = str.toCharArray();
for (char ch : arr) {
if (map.containsKey(ch)) {
Integer old = map.get(ch);
map.put(ch, old + 1);
} else {
map.put(ch,1);
}
}
return map;
}
public static void main(String[] args) {
String str = "aabcaacaahdbsc";
System.out.println(count(str)); //{a=6, b=2, c=3, s=1, d=1, h=1}
//创建Map集合,存放调用count()方法的返回值
Map<Character, Integer> map = new HashMap<Character, Integer>();
//将返回值put到集合当中
map.putAll(count(str));
//根据key查看对用的value值,例如a=6
int you = map.get('a');
System.out.println(you); //6
}
}