1. 程式人生 > >java中英文獲取首字母之後分組排序

java中英文獲取首字母之後分組排序

package com.syz;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author shiyz
 * @creation date 2012-10-25 上午13:01:21
 *
 */
public class Sort {
	public static void main(String[] args) {
		Sort obj1 = new Sort();
		System.out.println("======================");
		ArrayList list=new ArrayList();
		list.add("adisen");
		list.add("bulsi");
		list.add("Kobe");
		list.add("布丁");
		list.add("杜甫");
		list.add("元方");
		Map map=obj1.sort(list);
		System.out.println("-------分組後的輸出-----------");
        System.out.println(map.get("A"));
        System.out.println(map.get("B"));
        System.out.println(map.get("C"));
        System.out.println(map.get("D"));
        System.out.println(map.get("Y"));
	  } 
        public Sort() {
 	 
        }
	     //字母Z使用了兩個標籤,這裡有27個值
	 	 //i, u, v都不做聲母, 跟隨前面的字母
	    private char[] chartable =
           {
             '啊', '芭', '擦', '搭', '蛾', '發', '噶', '哈', '哈',
             '擊', '喀', '垃', '媽', '拿', '哦', '啪', '期', '然',
             '撒', '塌', '塌', '塌', '挖', '昔', '壓', '匝', '座'
            };
	    private char[] alphatableb =
          {
 	         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
             'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
 	       };
	    private char[] alphatables =
          {
 	         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
             'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
 	       };
	    private int[] table = new int[27];  //初始化
	      {
	 	         for (int i = 0; i < 27; ++i) {
	 	             table[i] = gbValue(chartable[i]);
	 	         }
	       }
		//主函式,輸入字元,得到他的聲母,
		//英文字母返回對應的大小寫字母
	    //其他非簡體漢字返回 '0'  按引數
	     public char Char2Alpha(char ch,String type) {
	          if (ch >= 'a' && ch <= 'z')
	              return (char) (ch - 'a' + 'A');//為了按字母排序先返回大寫字母
	           // return ch;
	          if (ch >= 'A' && ch <= 'Z')
	              return ch;

	             int gb = gbValue(ch);
	 	         if (gb < table[0])
	              return '0';
	  
	          int i;
	 	         for (i = 0; i < 26; ++i) {
	              if (match(i, gb))
	 	                 break;
	          }
	 	 
	 	         if (i >= 26){
	              return '0';}
	 	         else{
	 	        	 if("b".equals(type)){//大寫
	                     return alphatableb[i];
	                 }else{//小寫
	                	 return alphatables[i];
	                 }
	 	         }
	      }
	 //根據一個包含漢字的字串返回一個漢字拼音首字母的字串
     public String String2Alpha(String SourceStr,String type) {
         String Result = "";
         int StrLength = SourceStr.length();
         int i;
      try {
          for (i = 0; i < StrLength; i++) {
                 Result += Char2Alpha(SourceStr.charAt(i),type);
             }
         } catch (Exception e) {
          Result = "";
         }
      return Result;
    }
   //根據一個包含漢字的字串返回第一個漢字拼音首字母的字串
     public String String2AlphaFirst(String SourceStr,String type) {
           String Result = "";
         try {
           Result += Char2Alpha(SourceStr.charAt(0),type);
         } catch (Exception e) {
           Result = "";
         }
      return Result;
    }
     private boolean match(int i, int gb) {
            if (gb < table[i])
               return false;
 	         int j = i + 1;
  	 
 	         //字母Z使用了兩個標籤
  	         while (j < 26 && (table[j] == table[i]))
 	             ++j;
 	         if (j == 26)
 	             return gb <= table[j];
            else
 	             return gb < table[j];
  	      }
    	  	 
     //取出漢字的編碼
     private int gbValue(char ch) {
	     String str = new String();
	     str += ch;
	     try {
	         byte[] bytes = str.getBytes("GBK");
	             if (bytes.length < 2)
	                 return 0;
	             return (bytes[0] << 8 & 0xff00) + (bytes[1] &
	                     0xff);
	         } catch (Exception e) {
	           return 0;
	         }
	     }
     public Map sort(List list){
    	 Map map=new HashMap();
    	 ArrayList arraylist=new ArrayList();
    	 String[] alphatableb =
             {
    	        "A", "B", "C", "D", "E", "F", "G", "H", "I",
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
    	       };
    		 for(String a:alphatableb){
    			 for(int i=0;i<list.size();i++){//為了排序都返回大寫字母
	    			 if(a.equals(String2AlphaFirst(list.get(i).toString(),"b"))){
	    				 arraylist.add(list.get(i).toString());
	    			 }
    		     }
    			 map.put(a,arraylist);
    			 arraylist=new ArrayList();
    	 }
    	 return map;
     }
}