package cn.collection.test;
import java.util.Map;
import java.util.TreeMap;
/*
* 获取字符串中每个字母出现的次数
* dfklmaklkjgrad
* 以a(2)b(1)....
*/
public class MapTest {
public static void main(String[] args) {
String str = "dfklmaklkjgrad";
Map map = new TreeMap();
for (int i = 0; i < str.length(); i++) {
char temp = str.charAt(i);
if (map.containsKey(temp)) {
int t = (int) map.get(temp);
map.put(temp, t + 1);
} else {
map.put(temp, 1);
}
}
for (Object o : map.keySet()) {
char t = (char) o;
int j = (int) map.get(t);
System.out.print(t+"("+j+")");
}
// System.out.println(map);
}
}
获取字符串中每个字母出现的次数
最新推荐文章于 2023-03-20 19:55:39 发布