import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class StringTest {
public static void main(String[] args) {
String s="abcdefabced";
char[] ch=s.toCharArray();
TreeMap<Character,Integer> hm = new TreeMap<Character,Integer>();
for(int x=0;x<ch.length;x++)
{
Character c=ch[x];
Integer value=hm.get(c);
if(value!=null)
{
//键相同 那么把值+1就ok
value=value+1;
hm.put( c,value);
}
else
{//如果为空值就存1
hm.put(c, 1);
}
}
Set<Map.Entry<Character, Integer>> set = hm.entrySet();
Iterator<Map.Entry<Character, Integer>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<Character, Integer> me = it.next();
Character key = me.getKey();//键
Integer valu = me.getValue();//值得取出
System.out.print(key + "(" + valu + ")");// 按规定的格式输出
}
}
}
|