对map中的value进行排序

//对map中的value进行排序
public class SortMapValue {
	public static void main(String[] args) {
		Map<String,Integer> map=new LinkedHashMap<String,Integer>();
		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<String,Student> map1=new HashMap<String,Student>();
		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<String, Student> entry:map1.entrySet()){
			System.out.println(entry.getKey()+"#"+entry.getValue());
		}
		
	}
	/**
	 * clazz表示要比较的类型,比如String,Integer基本类型,或者对象Student
	 * fields:如果是对象的话,表示要比较该对象的哪个字段,多个要比较多个属性的话写上多个,前后的顺序是比较的优先级
	 * 如果是基本类型的话fields可以不写
	 * 如果是对象比较的话需要一个默认的构造方法
	 * 传入的map必须为linkedhashmap,或者的话传入的即使排序好也会乱掉
	 */
	public static<K,V> Map<K,V> sortMapValue(Map<K,V> map,final String...fields){
		Map<K,V> tempmap=new LinkedHashMap<K,V>();
		Set<Entry<K,V>> set=new TreeSet<Entry<K,V>>(new Comparator<Entry<K,V>>() {
			@Override
			public int compare(Entry<K, V> o1, Entry<K, V> 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<K, V> entry:map.entrySet()){
			set.add(entry);
		}
		map.clear();
		for(Entry<K,V> 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 <V>值的类型
 * @param v1 值1
 * @param type1 值的
 * @param v2
 * @param type2
 * @return
 */
public static<V> 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;
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值