需求背景
最近在做的一個項目裏,有需要展示我的通訊錄功能,與前端溝通結果是我將通訊錄裏所有聯繫人全部在接口裏返給他們,當然返回的數據格式是按照首字母已經歸類好了的,整體返回對象是一個Map<String,List>,key值就是首字母firstChar,value是所有首字母的集合list
具體實戰
pinyinUtil
在錄入通訊錄的時候, 根據錄入的中文服務端在入庫的時候取到中文對應的首字母存到表裏,這裏有一個jar包已經有很好的封裝方法。首先要引入
<!--漢語拼音-->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
然後看這裏提供的兩個方法,一個是將中文轉拼音,這個有可能有這樣的業務場景,另一個就是根據輸入字符串取首字母了,裏面邏輯還可以根據具體需求進行修改,我這裏是將特殊字符和數字全部按照#號處理,消防微信做的
import lombok.extern.slf4j.Slf4j;
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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
* @author zhangyu
* @description 漢語拼音工具類
* @date 2019/5/23 17:42
*/
@Slf4j
public class PinyinUtil {
/**
* 將文字轉爲漢語拼音
* @param chineseLanguage 要轉成拼音的中文
*/
public static String convertToPinyin(String chineseLanguage){
char[] cl_chars = chineseLanguage.trim().toCharArray();
String pinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
// 輸出拼音全部小寫
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
// 不帶聲調
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
try {
for (int i=0; i<cl_chars.length; i++){
// 如果字符是中文,則將中文轉爲漢語拼音
if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")){
pinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
} else {
// 如果字符不是中文,則不轉換
pinyin += cl_chars[i];
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
log.info("字符:{},轉拼音異常,原因爲{}",chineseLanguage,e);
}
return pinyin;
}
/**
* 取第一個漢字的第一個字符
* @Title: getFirstLetter
* @Description: TODO
* @return String
* @throws
*/
public static String getFirstLetter(String chineseLanguage){
char[] cl_chars = chineseLanguage.trim().toCharArray();
String pinyin = "";
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
// 輸出拼音全部大寫
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
// 不帶聲調
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
try {
String str = String.valueOf(cl_chars[0]);
// 如果字符是中文,則將中文轉爲漢語拼音,並取第一個字母
if (str.matches("[\u4e00-\u9fa5]+")) {
pinyin = PinyinHelper.toHanyuPinyinStringArray(cl_chars[0], defaultFormat)[0].substring(0, 1);
} else if (str.matches("[0-9]+")) {
// 如果字符是數字,取數字
//pinyin += cl_chars[0];
//本次需求數字變成#
return "#";
} else if (str.matches("[a-zA-Z]+")) {
// 如果字符是字母,取字母
pinyin += cl_chars[0];
} else {
// 否則返回'#'
return "#";
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
log.info("字符:{},轉拼音異常,原因爲{}",chineseLanguage,e);
}
if(chineseLanguage.startsWith("重慶") || chineseLanguage.startsWith("長沙")
|| chineseLanguage.startsWith("長春") ||chineseLanguage.startsWith("長治")){
return "C";
}
if(chineseLanguage.startsWith("廈門")){
return "X";
}
return pinyin;
}
public static void main(String[] args) {
System.out.println(convertToPinyin("張飛"));
System.out.println(getFirstLetter("長沙市"));
}
}
有了獲取首字母入庫的前提條件後,再返回前端通訊錄時,按照前端夥伴的需求將結果按照首字母歸類返回
public Map<String,List<AddressBookVO>> list() {
//僞代碼需要自己實現
List<AddressBookVO> addressBookVOS = selectAll();
//以首字母分組返回
Map<String,List<AddressBookVO>> resultMap = new HashMap<>();
if (CollectionUtil.isNotEmpty(addressBookVOS)) {
for(AddressBookVO temp : addressBookVOS){
//map中已存在
if(resultMap.containsKey(temp.getFirstChar())){
resultMap.get(temp.getFirstChar()).add(temp);
}else{
//map中不存在,新建key,用來存放數據
List<AddressBookVO> tmpList = new ArrayList<>();
tmpList.add(temp);
resultMap.put(temp.getFirstChar(), tmpList);
}
}
}
return resultMap;
}
上面這個方法,同樣適用於在一個list裏面按照某個字段進行分類的場景。