123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package com.yihu.jw.utils;
- 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 java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- public class PinYinUtil {
- /**
- * 获取汉字串拼音首字母,英文字符不变
- * @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 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();
- }
- /**
- * 字符串按拼音排序
- * @param list
- * @return
- */
- public static Map<String,List<Object>> pingYinSortByString(List<String> list){
- Map<String,List<Object>> map = new LinkedHashMap<>();
- String[] 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"};
- for (String alpha : alphatables){
- for(String one:list){
- String firstSpell = PinYinUtil.getFirstSpell(one).substring(0,1);
- if (alpha.equals(firstSpell)){
- List<Object> resultList = null;
- if(map.get(alpha)==null){
- resultList = new ArrayList();
- }else{
- resultList = map.get(alpha);
- }
- resultList.add(one);
- map.put(alpha,resultList);
- }
- }
- }
- return map;
- }
- /**
- * 字符串按拼音排序
- * @param
- * @return
- */
- public static Map<String,Object> pingYinSort(List<Map<String,Object>> result){
- Map<String,Object> map = new LinkedHashMap <>();
- String[] 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"};
- if (result.size() > 0) {
- for (String alpha : alphatables){
- List<Map<String,Object>> resultList = new ArrayList<>();
- for (Map<String,Object> one : result){
- String name = String.valueOf(one.get("name"));
- String firstSpell = PinYinUtil.getFirstSpell(name).substring(0,1);
- if (alpha.equals(firstSpell)){
- resultList.add(one);
- map.put(alpha,resultList);
- }
- }
- }
- return map;
- } else {
- return map;
- }
- }
- }
|