1、在pom文件中导入pinyin4j的依赖
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
2、汉字转拼音实现方法
2.1 获取首拼
public static String getStartPinYin(String str){
if (StringUtils.isEmpty(str)){
return "#";
}
//获取第一个字母或者汉子
char charAt = str.charAt(0);
for (int i = 0;i<str.length();i++){
char c = str.charAt(i);
if (StringUtils.isAlpha(c+"")){
charAt = c;
break;
}
}
if (Character.isLowerCase(charAt) || Character.isUpperCase(charAt)){
return Character.toUpperCase(charAt)+"";
}
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
String pinYin = null;
try {
pinYin = PinyinHelper.toHanYuPinyinString(str, format, "", false);
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
//badHanyuPinyinOutputFormatCombination.printStackTrace();
return "#";
}
if (StringUtils.isEmpty(pinYin)){
return "#";
}
return pinYin.substring(0,1).toUpperCase();
}
2.2 多音字,获取多音字的所有拼音
public static Map.Entry<String, String> converterToSpell(String chines) {
if (StringUtils.isBlank(chines)){
return new AbstractMap.SimpleEntry("#", "");
}
StringBuffer pinyinName = new StringBuffer();
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
// 取得当前汉字的所有全拼
String[] strs = PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat);
if (strs != null) {
for (int j = 0; j < strs.length; j++) {
pinyinName.append(strs[j]);
if (j != strs.length - 1) {
pinyinName.append(",");
}
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName.append(nameChar[i]);
}
pinyinName.append(" ");
}
// return pinyinName.toString();
String s = parseTheChineseByObject(discountTheChinese(pinyinName.toString()));
return getSortFirst(s);
}
private static String parseTheChineseByObject(
List<Map<String, Integer>> list) {
Map<String, Integer> first = null; // 用于统计每一次,集合组合数据
// 遍历每一组集合
for (int i = 0; i < list.size(); i++) {
// 每一组集合与上一次组合的Map
Map<String, Integer> temp = new Hashtable<String, Integer>();
// 第一次循环,first为空
if (first != null) {
// 取出上次组合与此次集合的字符,并保存
for (String s : first.keySet()) {
for (String s1 : list.get(i).keySet()) {
String str = s + s1;
temp.put(str, 1);
}
}
// 清理上一次组合数据
if (temp != null && temp.size() > 0) {
first.clear();
}
} else {
for (String s : list.get(i).keySet()) {
String str = s;
temp.put(str, 1);
}
}
// 保存组合数据以便下次循环使用
if (temp != null && temp.size() > 0) {
first = temp;
}
}
String returnStr = "";
if (first != null) {
// 遍历取出组合字符串
for (String str : first.keySet()) {
returnStr += (str + ",");
}
}
if (returnStr.length() > 0) {
returnStr = returnStr.substring(0, returnStr.length() - 1);
}
return returnStr;
}
private static Map.Entry<String, String> getSortFirst(String arrayStr){
Map.Entry<String, String> entryMap = new AbstractMap.SimpleEntry("#", "");
if (StringUtils.isNotBlank(arrayStr)){
if (arrayStr.contains(",")){
TreeMap<String,String> sortMap = new TreeMap<>();
String[] split = arrayStr.split(",");
for (int i = 0; i < split.length; i++) {
//获取首字母排序
sortMap.put(getStartPinYin(split[i]),split[i].toLowerCase());
}
Map.Entry<String, String> entry = sortMap.pollFirstEntry();
return entry;
}else {
entryMap = new AbstractMap.SimpleEntry(getStartPinYin(arrayStr), arrayStr);
/*map.put("allPinYin",arrayStr);
map.put("firstStart",getStartPinYin(arrayStr));*/
}
}
return entryMap;
}