- package com.wxy;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.TreeMap;
- public class Test9
- {
- public static void main(String[] args)
- {
- String str = "abcdekka27qoq";
- TreeMap<Character, Integer> map = new TreeMap<Character, Integer>();
- StringBuilder sb = new StringBuilder();
- char[] ch = str.toCharArray();
-
- for(int i=0;i<ch.length;i++)
- {
-
- if(!(ch[i]>='a' && ch[i]<='z' || ch[i]>='A' && ch[i]<='Z')) //判断是否是字母,不是字母重新开始for循环
- continue;
- Integer value = map.get(ch[i]);
- if(value == null)
- map.put(ch[i], 1);
- else
- {
- value++;
- map.put(ch[i],value);
- }
-
- }
- // Iterator<Character> it = map.keySet().iterator();
- // while(it.hasNext())
- // {
- // Character key = it.next();
- // Integer value = map.get(key);
- // sb.append(key +"(" + value + ")");
- // }
-
- Iterator<Map.Entry<Character, Integer>> it = map.entrySet().iterator();
- while(it.hasNext())
- {
- Map.Entry<Character, Integer> entry = it.next();
- Character key = entry.getKey();
- Integer value = entry.getValue();
- sb.append(key +"(" + value + ")");
- }
-
- System.out.println(sb.toString());
-
-
-
- }
-
- }
复制代码 |
|