public class Test1{
public static void main(String [] args){
String str = "abcdekka27qoq";
Map<Character, Integer> map = new TreeMap<Character, Integer>();
map = strToMap(map,str);
printMap(map);
}
public static Map<Character, Integer> strToMap(Map<Character, Integer> map,String str){
char [] chs = str.toCharArray();
for(int index=0;index<chs.length;index++){
if(!(map.containsKey(chs[index]))){
map.put(chs[index],1);
}
else{
int value = map.get(chs[index]);
map.put(chs[index],++value);
}
}
return map;
}
public static void printMap(Map<Character,Integer> map){
Set<Map.Entry<Character,Integer>> set = map.entrySet();
Iterator<Map.Entry<Character,Integer>> it = set.iterator();
while(it.hasNext()){
char key = it.next().getKey();
int value = it.next().getValue();
System.out.println(key+"("+value+")");
}
}
} |
|