List,map排序
来源:互联网 发布:六韬淘宝天猫托管 编辑:程序博客网 时间:2024/05/17 08:47
一、List排序
List排序要求每个元素必须实现comparable接口,具体Java的8中包装类型都已经实现了该接口。
两种方法:Collections.sort(Collection c)
Collections.sort(Collection c,Comparator cmp)
对汉字进行按照拼音排序
Collections.sort(list,Collator.getInstance(Locale.CHINA));
1、List排序中对一般字段排序,使用默认的方法直接进行排序:
/** * list的默认排序方法 * @param list 要排序的list * @param type 升序/降序 */ public static void defaultListSort(ArrayList<String> lists,TypeEnum type){ switch (type) { case ASC: Collections.sort(lists); //升序 break; case DESC:Collections.reverse(lists); //降序 default:Collections.reverse(lists); //降序 break; } for (String list : lists) { System.out.println(list); } } /** * 枚举,规定type的取值 * @author zhaojw_420 * */ public enum TypeEnum{ ASC,DESC; }
2、对某一个实体类进行排序,需要重写实体类的compareTo()方法:
public class CollectionSort { /** * 对实体类people进行排序 * @param lists * @param type */ public static void customListSort(ArrayList<People> lists,TypeEnum type){ switch (type) { case ASC: Collections.sort(lists); //升序 break; case DESC:Collections.reverse(lists); //降序 default:Collections.reverse(lists); //降序 break; } for (People list : lists) { System.out.println(list.toString()); } }/** * 枚举,规定type的取值 * @author zhaojw_420 * */ public enum TypeEnum{ ASC,DESC; } }class People implements Comparable<People>{ private String name; private int age; public People() { } public People(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "People [名字=" + name + ", 年龄=" + age + "]"; } //先以年龄排序,后以名字排序 public int compareTo(People people) { // TODO Auto-generated method stub if(this.age!=people.age){ return this.age-people.age; } return this.name.compareTo(people.name); }}
二、Map排序
1、可以使用TreeMap进行排序,TreeMap 里面的元素会自动排序,排序要求里面的元素的key是实现了comparable接口,也就是可以比较。Key一般都是String,所以一般map排序都是按照字符串的顺序。
/** * map按key来排序 * @param map 要排序的map * @param type 升序/降序 * 若map的可以得类型是一个people类,需要实现people的compareTo方法,再进行比较 */ public static void mapSortByKey(HashMap<String, String> map,final TypeEnum type){ if (map!=null&&!map.isEmpty()) { Map<String, String> sortMap=new TreeMap<String,String>(new Comparator<String>() { public int compare(String o1,String o2) { int key1=0;int key2=0; key1=Integer.parseInt(isNumeric(o1)?o1:"0"); key2=Integer.parseInt(isNumeric(o2)?o2:"0"); int result=0; switch (type) { case ASC: result=key1-key2; break; case DESC: result=key2-key1; break; default:result=key1-key2; break; } return result; } }); sortMap.putAll(map); System.out.println(map); } }/** * 判断是否为数字 * @param str * @return */ public static boolean isNumeric(String str){ Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); } /** * 枚举,规定type的取值 * @author zhaojw_420 * */ public enum TypeEnum{ ASC,DESC; }
2、map按照value进行排序:
/** * map按value值排序 * @param map * @param type */ public static void mapSortByValue(HashMap<String, String> map ,final TypeEnum type){ Map<String, String> sortMap=new LinkedHashMap<String, String>(); if (map!=null&&!map.isEmpty()) { List<Map.Entry<String, String>> entryList=new ArrayList<Map.Entry<String,String>>(map.entrySet()); Collections.sort(entryList,new Comparator<Map.Entry<String, String>>() { public int compare(Entry<String, String> o1, Entry<String, String> o2) { // TODO Auto-generated method stub int v1=0;int v2=0; v1=Integer.parseInt(isNumeric(o1.getValue())?o1.getValue():"0"); v2=Integer.parseInt(isNumeric(o2.getValue())?o2.getValue():"0"); int result=0; switch (type) { case ASC: result=v1-v2; break; case DESC: result=v2-v1; break; default:result=v1-v2; break; } return result; } }); Iterator<Map.Entry<String, String>> iterator=entryList.iterator(); Map.Entry<String, String> tmpMap=null; while(iterator.hasNext()){ tmpMap=iterator.next(); sortMap.put(tmpMap.getKey(), tmpMap.getValue()); } } } /** * 判断是否为数字 * @param str * @return */ public static boolean isNumeric(String str){ Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); } /** * 枚举,规定type的取值 * @author zhaojw_420 * */ public enum TypeEnum{ ASC,DESC; }
0 0
- Map List排序
- List<Map> 排序
- List<Map> 自定义排序
- List<Map> 排序
- collection,list,map 排序
- List<Map> 排序
- List <Map> 排序详解
- java list<map> 排序
- List<Map>排序
- List,map排序
- List、MAP排序
- List Map sort 排序
- Map&List排序
- Collections排序List<Map>
- List<Map<>>排序
- List<Map>排序
- freemarker的list,map,排序
- List<Map<String,Object>>排序
- Selenium-CSS页面元素定位
- 国外电子书免费下载网站
- 用jQuery实现选tab项卡效果
- eclipse下调试jenkins插件
- BigDecimal格式化小数
- List,map排序
- DES加密
- 通知栏显示富文本和大图片
- OpenCV的Delaunay三角剖分和Voronoi图的实现
- 警告LNK4272库计算机类型“X86”与目标计算机类型“x64”冲突
- 电子工程师最关心的12个问题
- oracle插入数据对比
- 在实际应用中认识IO模型
- MJ-CALayer简析