【列表操作】Java 实现 List 列表按照属性中的参数首字母进行排序

本贴最后更新于 2283 天前,其中的信息可能已经天翻地覆

说明

选取数据过多,导致前台页面杂乱,采用首字母分类,优化用户体验,下面是 java 实现 List 按字母分类的方法,代码是全的,大家可以复制下来运行看看结果,希望可以帮到大家,有什么不懂的地方,在评论区留言交流!

测试入口类:Test.java

import com.google.gson.Gson; import java.util.*; /** * 测试入口 * @author Moonce */ public class Test { public static void main(String[] args) { SortUtil obj1 = new SortUtil(); //获取默认数据 List list= getTestData(); //调用分类方法,返回map结果,结果格式:{A=[], B=[], C=[], D=[], E=[Symptom{id=3, typeId=0, name='恶心', remark='null'}], F=[], G=[], H=[], I=[], J=[], K=[], L=[], M=[], N=[], O=[Symptom{id=4, typeId=0, name='呕吐', remark='null'}], P=[], Q=[], R=[], S=[], T=[Symptom{id=1, typeId=0, name='头晕', remark='null'}, Symptom{id=2, typeId=0, name='头疼^', remark='null'}], U=[], V=[], W=[], X=[], Y=[], Z=[], 0=[]} Map map= obj1.sortSymptom(list); System.out.println("Map输出结果:"+map); //循环按照类别输出列表 for (Map.Entry entry : map.entrySet()) { List symptoms = (List) entry.getValue(); System.out.println(entry.getKey() +":" + symptoms); } // 用Gson转化为Json格式,结果格式:{"A":[],"B":[],"C":[],"D":[],"E":[{"id":3,"typeId":0,"name":"恶心"}],"F":[],"G":[],"H":[],"I":[],"J":[],"K":[],"L":[],"M":[],"N":[],"O":[{"id":4,"typeId":0,"name":"呕吐"}],"P":[],"Q":[],"R":[],"S":[],"T":[{"id":1,"typeId":0,"name":"头晕"},{"id":2,"typeId":0,"name":"头疼^"}],"U":[],"V":[],"W":[],"X":[],"Y":[],"Z":[],"0":[]} Gson gson = new Gson(); System.out.println("Json输出结果:"+gson.toJson(map));/**/ } /** * 自定义测试默认数据 * @return */ private static List getTestData() { List list=new ArrayList(); Symptom symptom = new Symptom(); symptom.setName("头晕"); symptom.setId(1L); list.add(symptom); symptom = new Symptom(); symptom.setName("头疼^"); symptom.setId(2L); list.add(symptom); symptom = new Symptom(); symptom.setName("恶心"); symptom.setId(3L); list.add(symptom); symptom = new Symptom(); symptom.setId(4L); symptom.setName("呕吐"); list.add(symptom); return list; } }

首字母分类工具类:SortUtil.java

package com.jeesite.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 症状按首字母分类方法 * @author Moonce */ public class SortUtil { public SortUtil() { } private final static String[] CAPITALIZATION_TABLE = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0" }; //字母Z使用了两个标签,这里有27个值 //i, u, v都不做声母, 跟随前面的字母 private char[] chartable = { '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈', '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座' }; private char[] alphatableb = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; private char[] alphatables = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; private int[] table = new int[27]; //初始化 { for (int i = 0; i < 27; ++i) { table[i] = gbValue(chartable[i]); } } /** * 主函数,输入字符,得到他的声母, * 英文字母返回对应的大小写字母 * 其他非简体汉字返回 '0' 按参数 * @param ch 首字母 * @param type b 大写 ,其他默认小写 * @return */ public char Char2Alpha(char ch,String type) { if (ch >= 'a' && ch <= 'z') return (char) (ch - 'a' + 'A');//为了按字母排序先返回大写字母 // return ch; if (ch >= 'A' && ch <= 'Z') return ch; int gb = gbValue(ch); if (gb < table[0]) return '0'; int i; for (i = 0; i < 26; ++i) { if (match(i, gb)) break; } if (i >= 26){ return '0';} else{ if("b".equals(type)){//大写 return alphatableb[i]; }else{//小写 return alphatables[i]; } } } /** * 根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串 * @param SourceStr 字符串 * @param type 返回类型 * @return */ public String String2Alpha(String SourceStr,String type) { String Result = ""; int StrLength = SourceStr.length(); int i; try { for (i = 0; i < StrLength; i++) { Result += Char2Alpha(SourceStr.charAt(i),type); } } catch (Exception e) { Result = ""; } return Result; } /** * 根据一个包含汉字的字符串返回第一个汉字拼音首字母的字符串 * @param SourceStr 字符串 * @param type 返回类型 * @return */ public String String2AlphaFirst(String SourceStr,String type) { String Result = ""; try { Result += Char2Alpha(SourceStr.charAt(0),type); } catch (Exception e) { Result = ""; } return Result; } private boolean match(int i, int gb) { if (gb < table[i]) return false; int j = i + 1; //字母Z使用了两个标签 while (j < 26 && (table[j] == table[i])) ++j; if (j == 26) return gb <= table[j]; else return gb < table[j]; } /** * 取出汉字的编码 * @param ch * @return */ private int gbValue(char ch) { String str = new String(); str += ch; try { byte[] bytes = str.getBytes("GBK"); if (bytes.length < 2) return 0; return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff); } catch (Exception e) { return 0; } } /** * 症状列表分类方法 * @param list * @return */ public Map sortSymptom(List list){ Map map=new HashMap(); List arraylist=new ArrayList(); for(String a:CAPITALIZATION_TABLE){ for(int i=0;i//为了排序都返回大写字母 if(a.equals(String2AlphaFirst(list.get(i).getName().toString(),"b"))){ arraylist.add(list.get(i)); } } map.put(a,arraylist); arraylist=new ArrayList(); } return map; } }

实体类:Symptom.java

package com.jeesite.test; /** * 症状实体类 * @author Moonce */public class Symptom { private long id; private long typeId; private String name; private String remark; public Symptom() { } @Override public String toString() { return "Symptom{" + "id=" + id + ", typeId=" + typeId + ", name='" + name + '\'' + ", remark='" + remark + '\'' + '}'; } public long getId() { return id; } public void setId(long id) { this.id = id; } public long getTypeId() { return typeId; } public void setTypeId(long typeId) { this.typeId = typeId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }

参考

java 中文首字母分组,排序

  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3196 引用 • 8215 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...