//返回实体
public class FriendPO {
@ApiModelProperty("用户id")
private Long userId;
@ApiModelProperty(value = "头像url")
private String avatar;
@ApiModelProperty(value = "昵称")
private String nickName;
@ApiModelProperty(value = "已有宝物数量")
private Integer baoNum;
@ApiModelProperty("备注")
private String remark;
}
/**
* @description: 排序好友列表, 按照昵称分组
* @params:
* @return:
*/
public TreeMap<String, List<FriendPO>> sortByLetterAsc(List<FriendPO> list) {
//按拼音首字母表排序
TreeMap<String, List<FriendPO>> letterMap = new TreeMap<>((s1, s2) -> {
//#号组放到最后
if ("#".equals(s1) ) {
return 1;
}
if ("#".equals(s2) ) {
return -1;
}
return s1.compareTo(s2);
});
if (!CollectionUtils.isEmpty(list)) {
for (FriendPO t : list) {
String pinYinFirstLetter = PinYinUtil.getPinYinFirstCharFirstLetter(t.getNickName());
if (!letterMap.containsKey(pinYinFirstLetter)) {
if (pinYinFirstLetter.matches("[A-Z]")) {
letterMap.put(pinYinFirstLetter, new ArrayList<FriendPO>());
} else {
letterMap.put("#", new ArrayList<FriendPO>());
}
}
}
for (Map.Entry<String, List<FriendPO>> next : letterMap.entrySet()) {
List<FriendPO> listTemp = new ArrayList<FriendPO>();
for (FriendPO t : list) {
String pinYinFirstLetter = PinYinUtil.getPinYinFirstCharFirstLetter(t.getNickName());
if (next.getKey().equals("#")) {
if (StringUtils.isNotBlank(pinYinFirstLetter) && !pinYinFirstLetter.matches("[A-Z]|[a-z]")) {
listTemp.add(t);
continue;
}
}
if (StringUtils.isNotBlank(pinYinFirstLetter) && next.getKey().equalsIgnoreCase(pinYinFirstLetter)) {
listTemp.add(t);
}
}
List<FriendPO> value = next.getValue();
value.addAll(listTemp);
}
}
return letterMap;
}
/**
* @description: 将字符串转成拼音,只要首字符的首字母
* @params:
* @return:
*/
public static String getPinYinFirstCharFirstLetter(String chinese) {
if (StringUtils.isBlank(chinese)){
return "";
}
char spell = chinese.toCharArray()[0];
String pinYin = String.valueOf(spell);
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
//大写
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
//无语调
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
if (spell > 128) {
try {
String[] strings = PinyinHelper.toHanyuPinyinStringArray(spell, defaultFormat);
if (ArrayUtils.isNotEmpty(strings)) {
pinYin = String.valueOf(strings[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
log.error(pinYin + "转换错误,返回原值");
return pinYin;
}
}
return pinYin;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15560.html