(9)Map按键和按键值排序

Map按键升序排序可以直接new一个TreeMap对象就可以实现了,要按键降序的话,需要重构TreeMap的Comparator比较器。这里给出了两种Map按值排序的方式。


import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;

public class SortMap {

    public static void main(String args[]){       
        //Map按键排序
        //Map中的TreeMap默认按照键升序排序
        Map<String, String> map1 = new TreeMap<String, String>();
        map1.put("b", "ccccc");
        map1.put("d", "aaaaa");
        map1.put("c", "bbbbb");
        map1.put("a", "ddddd");
        System.out.println("按键升序排序:"+map1);
        //通过是比较器Comparator,可以使Map按照键降序排序
        Map<String, String> map2 = new TreeMap<String, String>(
            new Comparator<String>() {
                public int compare(String obj1, String obj2) {
                    // 降序排序
                    return obj2.compareTo(obj1);
                }
            });
        map2.putAll(map1);
        System.out.println("按键降序排序:"+map2);      
        System.out.println("按键值升序排序:"+sortByValue(map1));   
        //按键值降序排序
        HashMap<String,Double> map = new HashMap<String,Double>();  
        ValueComparator bvc =  new ValueComparator(map);  
        TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);   
        map.put("A",99.5);  
        map.put("B",67.4);  
        map.put("C",67.4);  
        map.put("D",67.3);    
        System.out.println("unsorted map: "+map);   
        sorted_map.putAll(map);          
        System.out.println("results: "+sorted_map);  
        System.out.println(sortByValue(map));
    }

    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue( Map<K, V> map){  
        //将map.entrySet()转换成list
        LinkedList<Map.Entry<K, V>> list =  new LinkedList<Map.Entry<K, V>>( map.entrySet() );  
        //然后通过比较器来实现排序
        Collections.sort( list, new Comparator<Map.Entry<K, V>>() {  
            ////升序排序
            public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 ){  
                return (o1.getValue()).compareTo( o2.getValue() ); 
                //return (o2.getValue()).compareTo( o1.getValue() );//降序排序
            }  
        } );  

        Map<K, V> result = new LinkedHashMap<K, V>();  
        for (Map.Entry<K, V> entry : list) {  
            result.put( entry.getKey(), entry.getValue() );  
        }  
        return result;  
    }  

    static class ValueComparator implements Comparator<String> {  

        Map<String, Double> base;  
        public ValueComparator(Map<String, Double> base) {  
            this.base = base;  
        }  

        // 由大到小排序      
        public int compare(String a, String b) {  
            if (base.get(a) >= base.get(b)) {  
                return -1;  
            } else {  
                return 1;  
            } // returning 0 would merge keys  
        }  
    }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值