//定义一个List集合,泛型为String类型。统计整个集合中所有字符(注意,不是字符串)的个数。例如: //集合中有”abc”、”bcd”两个元素 //程序最终输出结果为: //a = 1 //b = 2 //c = 2 //d = 1 ------------------------------------------------------------------------------------------------- package com.heima.tests;
import java.security.KeyStore.Entry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test04 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("abc");
list.add("bcd");
StringBuffer sb = new StringBuffer();
for (String str : list) {
sb.append(str);
}
char[] chs = sb.toString().toCharArray();
HashMap<Character, Integer> hm = new HashMap<>();
for (char c : chs) {
if (!hm.containsKey(c)) {
hm.put(c, 1);
} else {
hm.put(c, hm.get(c) + 1);
}
}
for (Map.Entry<Character, Integer> c : hm.entrySet()) {
System.out.println(c);
}
}
}
|