Map根据Value排序

Map的特性

     Map是Key-Value的架构集合体,而Key属于Set的架构,也就是说Key值是唯一的,而Value值是可以重复;一般常用的Map结构是HashMap与TreeMap,其中TreeMap是Key有序的,如果需要Key有序则选择使用TreeMap,一般来说,考虑效率问题,则使用HashMap。

 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Demo {

	public static void main(String[] args) {
		Map<String, Integer> map_Data = new HashMap<String, Integer>();
		map_Data.put("A", 98);
		map_Data.put("B", 50);
		map_Data.put("C", 76);
		map_Data.put("D", 23);
		map_Data.put("E", 85);
		map_Data.put("F", 50);
		System.out.println(map_Data);
		//将Map转化为List集合,List采用ArrayList
		List<Map.Entry<String, Integer>> list_Data = new ArrayList<Map.Entry<String,Integer>>(map_Data.entrySet());
		//通过Collections.sort(List I,Comparator c)方法进行排序
                Collections.sort(list_Data,new Comparator<Map.Entry<String, Integer>>() {

			@Override
			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
				return (o1.getValue() - o2.getValue());
			}
		});
		System.out.println(list_Data);
	}
}

 结果如下:

{D=23, E=85, F=50, A=98, B=50, C=76}
[D=23, F=50, B=50, C=76, E=85, A=98]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值