package lianxi;
import java.util.HashMap;
import java.util.*;
import java.util.Map;
/**
 *
 * 如果一串字符串如"aaaabbbc中个512",分别统计英文字符,
 * 中文字符,和数字的字符的数量*
 *
 * 也可以自己进行一些排序,通过hashSet来实现和complaretor接口可以实现排序
 *
 * */
public class SubStrCount {
 
 
  private int digCount=0;
  private int englishCount=0;
  private int chinseCount=0;
   
   public Map digMap;
   public Map chinseMap;
   public Map englishMap;
  
   public static void main(String[]args){
   
    String tempstr="aaaabbbc中个512";
   
    SubStrCount test= new SubStrCount();
   
    test.getCount(tempstr);
   
    if(test.digMap!=null){
       System.out.println("数字的个数:"+(test.digMap.size()));
       System.out.println("---------------------------------");   
       test.println(test.digMap);
      
    }
    if(test.chinseMap!=null){
        System.out.println("中文的个数:"+(test.chinseMap.size()));
        System.out.println("---------------------------------");
        test.println(test.chinseMap);
    }
    if(test.englishMap!=null){
        System.out.println("字母的个数:"+(test.englishMap.size()));
        System.out.println("---------------------------------");
        test.println(test.englishMap);
    }
   
   }
  
  public void println(Map tempMap){
  
   if(tempMap!=null){
   
    Iterator tempiterator= tempMap.keySet().iterator();
   
    while(tempiterator.hasNext()){
    
     String tempchar=(String)tempiterator.next();
    
     System.out.println(tempchar+" :"+tempMap.get(tempchar));
    
    }
   
   }  
  }
 
 
   public void getCount(String str){  
    digMap=new HashMap();
    chinseMap=new HashMap();
    englishMap=new HashMap();   
   
    char []array=str.toCharArray();
   
    for(int i=0;i<array.length;i++){
    
     if((array[i]>='a' && array[i]<='z')||(array[i]>='A' && array[i]<='Z')){    
      addMap(englishMap,String.valueOf(array[i]));
      englishCount++;     
     }
     else if(array[i]>='0' && array[i]<='9'){
      addMap(digMap,String.valueOf(array[i]));
      digCount++;
     }
     else{
      addMap(chinseMap,String.valueOf(array[i]));
      chinseCount++;     
     }
    }   
   }
  
   public void addMap(Map tempMap,String tempChar){//根据不同的数据来存储
    if(tempMap!=null){
    
     Integer integer=(Integer)tempMap.get(tempChar);
    
     if(integer==null){  
     
      tempMap.put(tempChar,1);     
     }
     else{
     
      tempMap.put(tempChar,integer+1);
     }
    }
   
   }
  
}