package com.itcast.doudizhu;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class MapDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stu1
String s1 = "asdxbcvsdfaCCASASFAD";
System.out.println(count(s1));
}
public static String count(String str) {
TreeMap<Character, Integer> tmMap = new TreeMap<Character,Integer>();
char [] ch = str.toCharArray();
int count= 0 ;
for (int j = 0; j < ch.length; j++) {
Integer value = tmMap.get(ch[j]);
if (value != null){
count=value;
}
count++;
tmMap.put(ch[j], count);
count= 0;
}
//System.out.println(tmMap);
StringBuffer sBuffer =new StringBuffer();
Set<Map.Entry<Character, Integer>> set = tmMap.entrySet();
for (Map.Entry<Character, Integer> entry : set) {
Character c = entry.getKey();
Integer i = entry.getValue();
sBuffer.append(c).append("(").append(i).append(")");
}
return sBuffer.toString();
}
}
|
|