Map中根据value排序---输出value最大时所对应的Key

一   相关知识

        Map內涵资料是Key -> Value的架构集合体,而Key是属于Set的架构集合体,也就是说Key的值是唯一的,而Value的值可以重复。

        一般常用的子类是HashMap或TreeMap,

                           如果考虑效能的话,建议使用HashMap,

                           如果希望Key值有顺序性,就使用TreeMap吧!

二  根据Value的大小进行排序。(EntrySet+Comparator)

Map<String , Integer> mapInteger = new HashMap<String , Integer>();

mapInteger.put("A", 98);

mapInteger.put("B", 50);

mapInteger.put("C", 76);

mapInteger.put("D", 23);

mapInteger.put("E", 85);

map中对整数value值排序操作如下:

public static void mapSortInteger(Map<String , Integer> map){

List<Map.Entry<String, Integer>> listData = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());

System.out.println("排序前listData:");

System.out.println(listData);

//排序----// //当value值为整型时,  compare()方法

Collections.sort(listData, new Comparator<Map.Entry<String, Integer>>(){

public int compare(Map.Entry<String,Integer > o1, Map.Entry<String, Integer> o2){

return (o2.getValue() - o1.getValue());      

}

}

);

// //当value值为双精度浮点类型时,  compare()方法

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值