对Map中数据,按value值排序方法

1.Map<String,Integer>类型

		//声明
		Map<String,Integer> hashMap = new HashMap<String,Integer>();
		//向Map中添加数据
		//.....
		//转换
		ArrayList<Entry<String, Integer>> arrayList = new ArrayList<Entry<String, Integer>>(
					hashMap.entrySet());
		//排序
		Collections.sort(arrayList, new Comparator<Map.Entry<String, Integer>>() {
			public int compare(Map.Entry<String, Integer> map1,
						Map.Entry<String, Integer> map2) {
				return (map2.getValue() - map1.getValue());
			}
		});
		//输出
		for (Entry<String, Integer> entry : arrayList) {
			System.out.println(entry.getKey() + "\t" + entry.getValue());
		}

2.Map<String,Float>类型

		//声明
		Map<String,Float> hashMap = new HashMap<String,Float>();
		//向Map中添加数据
		//.....
		//转换
		ArrayList<Entry<String, Float>> arrayList = new ArrayList<Map.Entry<String,Float>>(hashMap.entrySet());
		//排序
		Collections.sort(arrayList, new Comparator<Map.Entry<String, Float>>(){
			public int compare(Map.Entry<String, Float> map1,
					Map.Entry<String,Float> map2) {
				return ((map2.getValue() - map1.getValue() == 0) ? 0
						: (map2.getValue() - map1.getValue() > 0) ? 1
								: -1);
			}
		});
		//输出
		for (Entry<String, Float> entry : arrayList) {
			System.out.println(entry.getKey() + "\t" + entry.getValue());
		}

3.Map<String,Double>类型

		//声明
		Map<String,Double> hashMap = new HashMap<String,Double>();
		//向Map中添加数据
		//.....
		//转换
		ArrayList<Entry<String, Double>> arrayList = new ArrayList<Map.Entry<String,Double>>(hashMap.entrySet());
		//排序
		Collections.sort(arrayList, new Comparator<Map.Entry<String, Double>>(){
			public int compare(Map.Entry<String, Double> map1,
					Map.Entry<String,Double> map2) {
				return ((map2.getValue() - map1.getValue() == 0) ? 0
						: (map2.getValue() - map1.getValue() > 0) ? 1
								: -1);
			}
		});
		//输出
		for (Entry<String, Double> entry : arrayList) {
			System.out.println(entry.getKey() + "\t" + entry.getValue());
		}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值