赞
踩
一、需要pinyin4j-2.5.0.jar
下载地址:Download pinyin4j-2.5.0.jar : pinyin4j « p « Jar File Download
二、引入
- <dependency>
- <groupId>net.sourceforge.pinyin4j</groupId>
- <artifactId>pinyin4j</artifactId>
- <version>2.5.0</version>
- </dependency>
三、建Util
- package com.wei.utils;
-
- import java.text.Collator;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Map;
- import java.util.TreeMap;
- 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;
-
- /**
- * @author mq
- * @date 2022/9/2 14:23
- */
- public class PinyinUtil {
-
- /**
- * 获取汉字串拼音,英文字符不变
- */
- public static String getFullSpell(String chinese) {
- StringBuffer pybf = new StringBuffer();
- char[] arr = chinese.toCharArray();
- HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
- defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
- defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
- for (int i = 0; i < arr.length; i++) {
- if (arr[i] > 128) {
- try {
- pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
- } catch (BadHanyuPinyinOutputFormatCombination e) {
- e.printStackTrace();
- }
- } else {
- pybf.append(arr[i]);
- }
- }
- return pybf.toString();
- }
-
- /**
- * 将集合数据按照汉字首字母分组排序
- */
- public static Map<String, Object> screenManufacturer(List<String> list) {
- try {
- Collator com = Collator.getInstance(java.util.Locale.CHINA);
- // 按字母排序
- Collections.sort(list, com);
- // 输出26个字母
- Map<String, Object> map = new TreeMap<>();
- for (int i = 1; i < 27; i++) {
- String word = String.valueOf((char)(96 + i)).toUpperCase();
- // 循环找出 首字母一样的数据
- List<String> letter = new ArrayList<>();
- for (String str : list) {
- String pybf = getFullSpell(str);
- String zm = pybf.substring(0, 1);
- if (word.equals(zm)) {
- letter.add(str);
- }
- }
- map.put(word, letter);
- }
- return map;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
- }
三、Test
- public static void main(String[] args){
- //模拟数据
- List<SysUser> sysUsers = new ArrayList<>();
- SysUser sysUser = new SysUser();
- sysUser.setId(1L);
- sysUser.setUsername("马强");
- sysUsers.add(sysUser);
- SysUser sysUser2 = new SysUser();
- sysUser2.setId(2L);
- sysUser2.setUsername("李强");
- sysUsers.add(sysUser2);
- SysUser sysUser3 = new SysUser();
- sysUser3.setId(3L);
- sysUser3.setUsername("啊强");
- sysUsers.add(sysUser3);
- SysUser sysUser4 = new SysUser();
- sysUser4.setId(4L);
- sysUser4.setUsername("阿妹");
- sysUsers.add(sysUser4);
- SysUser sysUser5 = new SysUser();
- sysUser5.setId(5L);
- sysUser5.setUsername("郑强");
- sysUsers.add(sysUser5);
-
- List<Map<String, Object>> mapList = new ArrayList<>();
- for (int i = 1; i < 27; i++) {
- Map<String, Object> map = new TreeMap<>();
- // 输出26个字母(大写)
- String letter = String.valueOf((char)(96 + i)).toUpperCase();
- //以此字母为首字母的数据集
- List<SysUser> users = new ArrayList<>();
- // 循环找出与首字母一样的数据
- for (SysUser user : sysUsers) {
- //获取汉字串拼音,英文字符不变
- String pybf = PinyinUtil.getFullSpell(user.getUsername());
- //首字母(大写)
- String Initials = pybf.substring(0, 1);
- if (letter.equals(Initials)) {
- users.add(user);
- }
- }
- map.put("key",letter);
- map.put("list", users);
- mapList.add(map);
- }
- System.out.println("排序后的数据为 ==> " + mapList.toString());
-
-
- }
效果:
- 排序后的数据为 == > [{
- key = A,
- list = [SysUser(id=3,username = 啊强), SysUser(id=4,username=阿妹)]
- }, {
- key = B,
- list = []
- }, {
- key = C,
- list = []
- }, {
- key = D,
- list = []
- }, {
- key = E,
- list = []
- }, {
- key = F,
- list = []
- }, {
- key = G,
- list = []
- }, {
- key = H,
- list = []
- }, {
- key = I,
- list = []
- }, {
- key = J,
- list = []
- }, {
- key = K,
- list = []
- }, {
- key = L,
- list = [SysUser(id=2,username = 李强)]
- }, {
- key = M,
- list = [SysUser(id=1,username = 马强]
- }, {
- key = N,
- list = []
- }, {
- key = O,
- list = []
- }, {
- key = P,
- list = []
- }, {
- key = Q,
- list = []
- }, {
- key = R,
- list = []
- }, {
- key = S,
- list = []
- }, {
- key = T,
- list = []
- }, {
- key = U,
- list = []
- }, {
- key = V,
- list = []
- }, {
- key = W,
- list = []
- }, {
- key = X,
- list = []
- }, {
- key = Y,
- list = []
- }, {
- key = Z,
- list = [SysUser(id=5,username = 郑强)]
- }]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。