对map中的value进行排序
发布日期:2021-09-16 12:20:04 浏览次数:5 分类:技术文章

本文共 3576 字,大约阅读时间需要 11 分钟。

//对map中的value进行排序public class SortMapValue {	public static void main(String[] args) {		Map
map=new LinkedHashMap
(); map.put("aaa", 45); map.put("e", 2); map.put("ww", 556); map.put("rr", 34); map.put("cs", 54); map.put("bs", 23); map.put("g", 66); Map
map1=new HashMap
(); map1.put("a", new Student("hj","cannan",23,891)); map1.put("t", new Student("tt","cannan",45,591)); map1.put("w", new Student("wj","cannan",21,291)); map1.put("g", new Student("gj","cannan",90,991)); map1.put("n", new Student("nj","cannan",7,191)); map1=sortMapValue(map1,"name","age"); for(Map.Entry
entry:map1.entrySet()){ System.out.println(entry.getKey()+"#"+entry.getValue()); } } /** * clazz表示要比较的类型,比如String,Integer基本类型,或者对象Student * fields:如果是对象的话,表示要比较该对象的哪个字段,多个要比较多个属性的话写上多个,前后的顺序是比较的优先级 * 如果是基本类型的话fields可以不写 * 如果是对象比较的话需要一个默认的构造方法 * 传入的map必须为linkedhashmap,或者的话传入的即使排序好也会乱掉 */ public static
Map
sortMapValue(Map
map,final String...fields){ Map
tempmap=new LinkedHashMap
(); Set
> set=new TreeSet
>(new Comparator
>() { @Override public int compare(Entry
o1, Entry
o2) { int valflag=0; final String ClassName=o1.getValue().getClass().getSimpleName();//得到V的类型 int keyflag=o1.getKey().toString().compareTo(o2.getKey().toString()); //比较基本类型和String类型 if(ClassName.equals("String")||ClassName.equals("Byte")||ClassName.equals("Character")||ClassName.equals("Short")|| ClassName.equals("Integer")||ClassName.equals("Long")||ClassName.equals("Float")||ClassName.equals("Double")){ valflag=vCompare(o1.getValue(), ClassName, o2.getValue(), ClassName); if(valflag!=0){ return valflag; }else{ return keyflag; } }else{//比较对象 if(fields!=null&&fields.length<=0){ return 0; } Class clazz1=o1.getValue().getClass(); Class clazz2=o2.getValue().getClass(); for(String field:fields){ try { Field f1=clazz1.getDeclaredField(field); Field f2=clazz2.getDeclaredField(field); f1.setAccessible(true); f2.setAccessible(true); valflag=vCompare(f1.get(o1.getValue()), f1.getType().getSimpleName(), f2.get(o2.getValue()), f2.getType().getSimpleName()); if(valflag!=0){ return valflag; }else{ return keyflag;//先假设只有一个比较参数 } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); }catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } return valflag; } }); for(Map.Entry
entry:map.entrySet()){ set.add(entry); } map.clear(); for(Entry
entry:set){// System.out.println("#"+entry.getKey()+":"+entry.getValue()); tempmap.put(entry.getKey(), entry.getValue()); map.put(entry.getKey(), entry.getValue());//如果是LinkedHashmap的话就不用在调用的时候赋值了,否则需要重新赋值 } return tempmap; }/**' * * @param
值的类型 * @param v1 值1 * @param type1 值的 * @param v2 * @param type2 * @return */public static
int vCompare(V v1,String type1,V v2,String type2){ int valflag=0; if(type1.equalsIgnoreCase("String")){ return v1.toString().compareTo(v2.toString()); }else if(type1.equalsIgnoreCase("Byte")||type1.equalsIgnoreCase("Character")||type1.equalsIgnoreCase("Short")||type1.equalsIgnoreCase("Integer") ||type1.equalsIgnoreCase("Long")||type1.equalsIgnoreCase("Float")||type1.equalsIgnoreCase("Double")||type1.equalsIgnoreCase("int") ||type1.equalsIgnoreCase("char")){ valflag=(int)(Double.parseDouble(v1.toString())-Double.parseDouble(v2.toString()));// System.out.println(v1.toString()+":"+v2.toString());// System.out.println(valflag+":"+(Double.parseDouble(v1.toString())+":"+Double.parseDouble(v2.toString()))); return valflag; } return 0;}}

转载地址:https://blog.csdn.net/wxwzy738/article/details/7486272 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:线程池类Executors的运用
下一篇:ThreadLocal的作用

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月11日 14时51分57秒