希望打印结果:a(1)c(2)...
- package com.mytest;
-
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeMap;
-
- public class test02 {
- public static void main(String[] args) {
-
- String str = "asdfaadsfsaa";
-
- System.out.println(charCount(str));
-
- }
-
- public static String charCount(String str) {
-
- char[] chs = str.toCharArray();
- Map map = new TreeMap();
-
- for (int x = 0; x < chs.length; x++) {
-
- char ch = chs[x];
-
- Integer value = map.get(ch);
- if (value == null) {
- map.put(ch, 1);
- } else {
- value = value + 1;
- map.put(ch, value);
- }
- }
-
- // System.out.println(map);
-
- Set> entrySet = map.entrySet();
-
- Iterator> it = entrySet.iterator();
- StringBuffer sb = new StringBuffer();
-
- while (it.hasNext()) {
- Map.Entry me = it.next();
- Character key = me.getKey();
- Integer value = me.getValue();
- sb.append(key + "(" + value + ")");
- }
-
- return sb.toString();
-
- }
-
- }
复制代码 |
|