加入拼音maven 坐标:
com.belerweb
pinyin4j
2.5.0
写工具类测试:
package com.jwork;
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.lang.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws Exception {
// String[] m = PinyinHelper.toHanyuPinyinStringArray('马');
//
// for (String s:m) {
// System.out.println(s);
// }
String[] arr = {"马小跳","穆老师","李清照","任旭剑","岑燕","常轻轻","岑安","2李清","2常轻轻","a岑安","安岑安"};
// Map> sort = sort(Arrays.asList(arr));
Map> sortMap = sortpinyin(Arrays.asList(arr));
System.out.println(sortMap);
// String mxt = getheadpinyin("马小跳");
// System.out.println("111="+mxt);
}
public static Map> sort(List list){
Map> rtMap = new HashMap<>();
Map> result=new LinkedHashMap();
for (String s : list) {
char[] arr = s.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
if (arr[0] > 128) {
try {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[0], defaultFormat);
if (temp != null) {
if (rtMap.containsKey(String.valueOf(temp[0].charAt(0)))) {
rtMap.get(String.valueOf(temp[0].charAt(0))).add(s);
} else {
List li=new ArrayList<>();
li.add(s);
rtMap.put(String.valueOf(temp[0].charAt(0)), li);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
if (rtMap.containsKey(String.valueOf(arr[0]))) {
rtMap.get(String.valueOf(arr[0])).add(s);
} else {
List li=new ArrayList<>();
li.add(s);
rtMap.put(String.valueOf(arr[0]), li);
}
}
result =rtMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
}
return result;
}
public static Map> sortpinyin(List list) throws Exception {
Map> result = new LinkedHashMap();
Map> map=new LinkedHashMap();
for (String name:list) {
String surname = getheadpinyin(name);
List nameList = map.get(surname.substring(0, 1));
if(nameList==null){
nameList = new ArrayList<>();
}
nameList.add(name);
map.put(surname.substring(0,1),nameList);
}
// value值遍历
for (String k:map.keySet()) {
List value = map.get(k);
value = value.stream().sorted().collect(Collectors.toList());
map.put(k,value);
}
result =map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
return result;
}
public static String getheadpinyin(String str)throws Exception{
if (StringUtils.isBlank(str))
{
return "";
}
StringBuilder sb= new StringBuilder();
char[] chars = str.toCharArray();
Character character =chars[0];
try {
// for (Character character: chars){
String[] strings = PinyinHelper.toHanyuPinyinStringArray(character);// 取每个字的拼,第一个字符转化为大写
// String string =strings[0].substring(0, 1).toUpperCase();
// sb.append(string);
sb.append(strings[0].toUpperCase());
// }
return sb.toString();
}catch(Exception e){
// throw new Exception("有非中文字符");
sb.append(character.toString().toUpperCase());
return sb.toString();
}
}
}
实战使用:
package gcloud.xyuser.util;
import com.crenjoy.fund.view.FundUserView;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.apache.commons.lang.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
public class PingYinUtil {
public static Map> sortPinYinUser(List list) throws Exception {
Map> result = new LinkedHashMap();
Map> map=new LinkedHashMap();
for (FundUserView userView:list) {
String name = userView.getRealName();
String surname = getheadpinyin(name);
List userList = map.get(surname.substring(0, 1));
if(userList==null){
userList = new ArrayList<>();
}
userList.add(userView);
map.put(surname.substring(0,1),userList);
}
// value值遍历
for (String k:map.keySet()) {
List value = map.get(k);
value = value.stream().sorted(Comparator.comparing(FundUserView::getRealName)).collect(Collectors.toList());
map.put(k,value);
}
result =map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
List otherUser = result.get("#");
result.remove("#");
result.put("#",otherUser);
return result;
}
public static Map> sortpinyin(List list) throws Exception {
Map> result = new LinkedHashMap();
Map> map=new LinkedHashMap();
for (String name:list) {
String surname = getheadpinyin(name);
List nameList = map.get(surname.substring(0, 1));
if(nameList==null){
nameList = new ArrayList<>();
}
nameList.add(name);
map.put(surname.substring(0,1),nameList);
}
// value值遍历
for (String k:map.keySet()) {
List value = map.get(k);
value = value.stream().sorted().collect(Collectors.toList());
map.put(k,value);
}
result =map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
return result;
}
public static String getheadpinyin(String str)throws Exception{
if (StringUtils.isBlank(str))
{
return "";
}
StringBuilder sb= new StringBuilder();
char[] chars = str.toCharArray();
Character character =chars[0];
try {
String[] strings = PinyinHelper.toHanyuPinyinStringArray(character);// 取每个字的拼,第一个字符转化为大写
sb.append(strings[0].toUpperCase());
return sb.toString();
}catch(Exception e){
sb.append("#");
return sb.toString();
}
}
}