当前位置:   article > 正文

将用户表数据按照用户姓名首字母进行分组排序输出-pinyin4j_net.sourceforge.pinyin4j 下载

net.sourceforge.pinyin4j 下载

一、需要pinyin4j-2.5.0.jar

下载地址:Download pinyin4j-2.5.0.jar : pinyin4j « p « Jar File Download

二、引入

  1. <dependency>
  2. <groupId>net.sourceforge.pinyin4j</groupId>
  3. <artifactId>pinyin4j</artifactId>
  4. <version>2.5.0</version>
  5. </dependency>

三、建Util

  1. package com.wei.utils;
  2. import java.text.Collator;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.TreeMap;
  8. import net.sourceforge.pinyin4j.PinyinHelper;
  9. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  10. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  11. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  12. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  13. /**
  14. * @author mq
  15. * @date 2022/9/2 14:23
  16. */
  17. public class PinyinUtil {
  18. /**
  19. * 获取汉字串拼音,英文字符不变
  20. */
  21. public static String getFullSpell(String chinese) {
  22. StringBuffer pybf = new StringBuffer();
  23. char[] arr = chinese.toCharArray();
  24. HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
  25. defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
  26. defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  27. for (int i = 0; i < arr.length; i++) {
  28. if (arr[i] > 128) {
  29. try {
  30. pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
  31. } catch (BadHanyuPinyinOutputFormatCombination e) {
  32. e.printStackTrace();
  33. }
  34. } else {
  35. pybf.append(arr[i]);
  36. }
  37. }
  38. return pybf.toString();
  39. }
  40. /**
  41. * 将集合数据按照汉字首字母分组排序
  42. */
  43. public static Map<String, Object> screenManufacturer(List<String> list) {
  44. try {
  45. Collator com = Collator.getInstance(java.util.Locale.CHINA);
  46. // 按字母排序
  47. Collections.sort(list, com);
  48. // 输出26个字母
  49. Map<String, Object> map = new TreeMap<>();
  50. for (int i = 1; i < 27; i++) {
  51. String word = String.valueOf((char)(96 + i)).toUpperCase();
  52. // 循环找出 首字母一样的数据
  53. List<String> letter = new ArrayList<>();
  54. for (String str : list) {
  55. String pybf = getFullSpell(str);
  56. String zm = pybf.substring(0, 1);
  57. if (word.equals(zm)) {
  58. letter.add(str);
  59. }
  60. }
  61. map.put(word, letter);
  62. }
  63. return map;
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. return null;
  68. }
  69. }

三、Test

  1. public static void main(String[] args){
  2. //模拟数据
  3. List<SysUser> sysUsers = new ArrayList<>();
  4. SysUser sysUser = new SysUser();
  5. sysUser.setId(1L);
  6. sysUser.setUsername("马强");
  7. sysUsers.add(sysUser);
  8. SysUser sysUser2 = new SysUser();
  9. sysUser2.setId(2L);
  10. sysUser2.setUsername("李强");
  11. sysUsers.add(sysUser2);
  12. SysUser sysUser3 = new SysUser();
  13. sysUser3.setId(3L);
  14. sysUser3.setUsername("啊强");
  15. sysUsers.add(sysUser3);
  16. SysUser sysUser4 = new SysUser();
  17. sysUser4.setId(4L);
  18. sysUser4.setUsername("阿妹");
  19. sysUsers.add(sysUser4);
  20. SysUser sysUser5 = new SysUser();
  21. sysUser5.setId(5L);
  22. sysUser5.setUsername("郑强");
  23. sysUsers.add(sysUser5);
  24. List<Map<String, Object>> mapList = new ArrayList<>();
  25. for (int i = 1; i < 27; i++) {
  26. Map<String, Object> map = new TreeMap<>();
  27. // 输出26个字母(大写)
  28. String letter = String.valueOf((char)(96 + i)).toUpperCase();
  29. //以此字母为首字母的数据集
  30. List<SysUser> users = new ArrayList<>();
  31. // 循环找出与首字母一样的数据
  32. for (SysUser user : sysUsers) {
  33. //获取汉字串拼音,英文字符不变
  34. String pybf = PinyinUtil.getFullSpell(user.getUsername());
  35. //首字母(大写)
  36. String Initials = pybf.substring(0, 1);
  37. if (letter.equals(Initials)) {
  38. users.add(user);
  39. }
  40. }
  41. map.put("key",letter);
  42. map.put("list", users);
  43. mapList.add(map);
  44. }
  45. System.out.println("排序后的数据为 ==> " + mapList.toString());
  46. }

效果:

  1. 排序后的数据为 == > [{
  2. key = A,
  3. list = [SysUser(id=3,username = 啊强), SysUser(id=4,username=阿妹)]
  4. }, {
  5. key = B,
  6. list = []
  7. }, {
  8. key = C,
  9. list = []
  10. }, {
  11. key = D,
  12. list = []
  13. }, {
  14. key = E,
  15. list = []
  16. }, {
  17. key = F,
  18. list = []
  19. }, {
  20. key = G,
  21. list = []
  22. }, {
  23. key = H,
  24. list = []
  25. }, {
  26. key = I,
  27. list = []
  28. }, {
  29. key = J,
  30. list = []
  31. }, {
  32. key = K,
  33. list = []
  34. }, {
  35. key = L,
  36. list = [SysUser(id=2,username = 李强)]
  37. }, {
  38. key = M,
  39. list = [SysUser(id=1,username = 马强]
  40. }, {
  41. key = N,
  42. list = []
  43. }, {
  44. key = O,
  45. list = []
  46. }, {
  47. key = P,
  48. list = []
  49. }, {
  50. key = Q,
  51. list = []
  52. }, {
  53. key = R,
  54. list = []
  55. }, {
  56. key = S,
  57. list = []
  58. }, {
  59. key = T,
  60. list = []
  61. }, {
  62. key = U,
  63. list = []
  64. }, {
  65. key = V,
  66. list = []
  67. }, {
  68. key = W,
  69. list = []
  70. }, {
  71. key = X,
  72. list = []
  73. }, {
  74. key = Y,
  75. list = []
  76. }, {
  77. key = Z,
  78. list = [SysUser(id=5,username = 郑强)]
  79. }]
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/596807
推荐阅读
相关标签
  

闽ICP备14008679号