PinYinUtil.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.yihu.jw.utils;
  2. import net.sourceforge.pinyin4j.PinyinHelper;
  3. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  6. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  7. import java.util.ArrayList;
  8. import java.util.LinkedHashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. public class PinYinUtil {
  12. /**
  13. * 获取汉字串拼音首字母,英文字符不变
  14. * @param chinese 汉字串
  15. * @return 汉语拼音首字母
  16. */
  17. public static String getFirstSpell(String chinese) {
  18. StringBuffer pybf = new StringBuffer();
  19. char[] arr = chinese.toCharArray();
  20. HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
  21. defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  22. defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  23. for (int i = 0; i < arr.length; i++) {
  24. if (arr[i] > 128) {
  25. try {
  26. String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
  27. if (temp != null) {
  28. pybf.append(temp[0].charAt(0));
  29. }
  30. } catch (BadHanyuPinyinOutputFormatCombination e) {
  31. e.printStackTrace();
  32. }
  33. } else {
  34. pybf.append(arr[i]);
  35. }
  36. }
  37. return pybf.toString().replaceAll("\\W", "").trim();
  38. }
  39. /**
  40. * 获取汉字串拼音,英文字符不变
  41. * @param chinese 汉字串
  42. * @return 汉语拼音
  43. */
  44. public static String getFullSpell(String chinese) {
  45. StringBuffer pybf = new StringBuffer();
  46. char[] arr = chinese.toCharArray();
  47. HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
  48. defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  49. defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  50. for (int i = 0; i < arr.length; i++) {
  51. if (arr[i] > 128) {
  52. try {
  53. pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
  54. } catch (BadHanyuPinyinOutputFormatCombination e) {
  55. e.printStackTrace();
  56. }
  57. } else {
  58. pybf.append(arr[i]);
  59. }
  60. }
  61. return pybf.toString();
  62. }
  63. /**
  64. * 字符串按拼音排序
  65. * @param list
  66. * @return
  67. */
  68. public static Map<String,List<Object>> pingYinSortByString(List<String> list){
  69. Map<String,List<Object>> map = new LinkedHashMap<>();
  70. String[] alphatables = {"a", "b", "c", "d", "e", "f", "g", "h", "i",
  71. "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
  72. for (String alpha : alphatables){
  73. for(String one:list){
  74. String firstSpell = PinYinUtil.getFirstSpell(one).substring(0,1);
  75. if (alpha.equals(firstSpell)){
  76. List<Object> resultList = null;
  77. if(map.get(alpha)==null){
  78. resultList = new ArrayList();
  79. }else{
  80. resultList = map.get(alpha);
  81. }
  82. resultList.add(one);
  83. map.put(alpha,resultList);
  84. }
  85. }
  86. }
  87. return map;
  88. }
  89. /**
  90. * 字符串按拼音排序
  91. * @param
  92. * @return
  93. */
  94. public static Map<String,Object> pingYinSort(List<Map<String,Object>> result){
  95. Map<String,Object> map = new LinkedHashMap <>();
  96. String[] alphatables = {"a", "b", "c", "d", "e", "f", "g", "h", "i",
  97. "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
  98. if (result.size() > 0) {
  99. for (String alpha : alphatables){
  100. List<Map<String,Object>> resultList = new ArrayList<>();
  101. for (Map<String,Object> one : result){
  102. String name = String.valueOf(one.get("name"));
  103. String firstSpell = PinYinUtil.getFirstSpell(name).substring(0,1);
  104. if (alpha.equals(firstSpell)){
  105. resultList.add(one);
  106. map.put(alpha,resultList);
  107. }
  108. }
  109. }
  110. return map;
  111. } else {
  112. return map;
  113. }
  114. }
  115. }