| package com.doumee.core.utils; | 
|   | 
| import java.text.Collator; | 
| import java.util.ArrayList; | 
| import java.util.Arrays; | 
| import java.util.Collections; | 
| import java.util.Comparator; | 
| import java.util.List; | 
| import java.util.Map; | 
|   | 
|   | 
| import net.sourceforge.pinyin4j.PinyinHelper; | 
| import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; | 
| import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; | 
| import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; | 
| import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; | 
| import org.apache.commons.lang3.StringUtils; | 
|   | 
| /** | 
|  * 汉语拼音帮助类 | 
|  *  | 
|  * @author yanglong | 
|  *  | 
|  */ | 
| public class PinYinUtil { | 
|   | 
|     /** | 
|      * 比较两个汉字的首字母 <br> | 
|      *  | 
|      * @param ch1 | 
|      * @param ch2 | 
|      * @return | 
|      */ | 
|     public static int comparatorWord(String ch1, String ch2) { | 
|         return getFirstSpell(ch2).compareTo(getFirstSpell(ch1)); | 
|     } | 
|   | 
|     public static void main(String[] args) { | 
|         Comparator<Object> com=Collator.getInstance(java.util.Locale.CHINA);   | 
|         String[] newArray={"中山","汕头","广州","安庆","阳江","南京","武汉","北京","安阳","北方"};   | 
|         List<String> list = Arrays.asList(newArray); | 
|         Collections.sort(list, com);  | 
|         for(String i:list){   | 
|             System.out.print(i+"  ");   | 
|         } | 
|         //System.err.println(getFirstFirstSpell("很合适的积分很少看见")); | 
|     } | 
|     /** | 
|      * 获取汉字串拼音首字母,英文字符不变 | 
|      *  | 
|      * @param chinese | 
|      *            汉字串 | 
|      * @return 汉语拼音首字母 | 
|      */ | 
|     public static String getFirstSpell(String chinese) { | 
|         StringBuffer pybf = new StringBuffer(); | 
|         char[] arr = chinese.toCharArray(); | 
|         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); | 
|         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); | 
|         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); | 
|         for (int i = 0; i < arr.length; i++) { | 
|             if (arr[i] > 128) { | 
|                 try { | 
|                     String[] temp = PinyinHelper.toHanyuPinyinStringArray( | 
|                             arr[i], defaultFormat); | 
|                     if (temp != null) { | 
|                         pybf.append(temp[0].charAt(0)); | 
|                     } | 
|                 } catch (BadHanyuPinyinOutputFormatCombination e) { | 
|                     e.printStackTrace(); | 
|                 } | 
|             } else { | 
|                 pybf.append(arr[i]); | 
|             } | 
|         } | 
|         return pybf.toString().replaceAll("\\W", "").trim(); | 
|     } | 
|     /** | 
|      * 获取汉字串拼音首字母,英文字符不变 | 
|      *  | 
|      * @param chinese | 
|      *            汉字串 | 
|      * @return 汉语拼音首字母 | 
|      */ | 
|     public static String getFirstFirstSpell(String chinese) { | 
|         if(StringUtils.isBlank(chinese)){ | 
|             return null; | 
|         } | 
|         StringBuffer pybf = new StringBuffer(); | 
|         chinese = chinese.substring(0,1); | 
|         char[] arr = chinese.toCharArray(); | 
|         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); | 
|         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); | 
|         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); | 
|         for (int i = 0; i < arr.length; i++) { | 
|             if (arr[i] > 128) { | 
|                 try { | 
|                     String[] temp = PinyinHelper.toHanyuPinyinStringArray( | 
|                             arr[i], defaultFormat); | 
|                     if (temp != null) { | 
|                         pybf.append(temp[0].charAt(0)); | 
|                     } | 
|                 } catch (BadHanyuPinyinOutputFormatCombination e) { | 
|                     e.printStackTrace(); | 
|                 } | 
|             } else { | 
|                 pybf.append(arr[i]); | 
|             } | 
|         } | 
|         return pybf.toString().replaceAll("\\W", "").trim().toUpperCase(); | 
|     } | 
|   | 
|     /** | 
|      * 获取汉字串拼音,英文字符不变 | 
|      *  | 
|      * @param chinese | 
|      *            汉字串 | 
|      * @return 汉语拼音 | 
|      */ | 
|     public static String getFullSpell(String chinese) { | 
|         StringBuffer pybf = new StringBuffer(); | 
|         char[] arr = chinese.toCharArray(); | 
|         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); | 
|         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); | 
|         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); | 
|         for (int i = 0; i < arr.length; i++) { | 
|             if (arr[i] > 128) { | 
|                 try { | 
|                     pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], | 
|                             defaultFormat)[0]); | 
|                 } catch (BadHanyuPinyinOutputFormatCombination e) { | 
|                     e.printStackTrace(); | 
|                 } | 
|             } else { | 
|                 pybf.append(arr[i]); | 
|             } | 
|         } | 
|         return pybf.toString(); | 
|     } | 
| } |