Map实现按照Key的升降序和按照Value的升降序

1.TreeMap按照Key排序,默认为升序
2.将TreeMap按照Key降序则需要传入Comparator对象,重写compare方法。
3.按照Value排序则需要将map.entrySet()转换为list。


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

public class mapSort {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        mapSort m = new mapSort();
        m.sortByKey();
        m.sortByKey_DES();
        m.sortByValue();
        m.sortByValue_DES();
    }
    //按照key进行排序 采用TreeMap 默认升序
    public void sortByKey() {
        System.out.println("测试按照Key升序");
        Map<String,Integer> map = new TreeMap<String,Integer>();
        map.put("hello",33);
        map.put("awake",320);
        map.put("yeah",78);
        for (Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
    }
    //按照key进行排序 采用TreeMap 降序
    public void sortByKey_DES() {
        System.out.println("测试按照Key降序");
        Map<String,Integer> map = new TreeMap<String,Integer>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                // TODO Auto-generated method stub
                return o2.compareTo(o1);
            }
        });
        map.put("hello",33);
        map.put("awake",320);
        map.put("yeah",78);
        for (Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
    }
    //按照value进行排序 采用TreeMap 升序
    public void sortByValue() {
        System.out.println("测试按照Value升序");
        Map<String,Integer> map = new TreeMap<String,Integer>();
        map.put("hello",33);
        map.put("awake",320);
        map.put("yeah",78);
        //将map.entrySet()转换为list
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        //通过集合的比较其来实现排序
        Collections.sort(list,new Comparator<Map.Entry<String, Integer>>() {

            @Override
            public int compare(Entry<String, Integer> o1,
                    Entry<String, Integer> o2) {
                // TODO Auto-generated method stub
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        for (Entry<String, Integer> entry :list) {
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
    }

    //按照value进行排序 采用TreeMap 降序
    public void sortByValue_DES() {
        System.out.println("测试按照Value降序");
        Map<String,Integer> map = new TreeMap<String,Integer>();
        map.put("hello",33);
        map.put("awake",320);
        map.put("yeah",78);
        //将map.entrySet()转换为list
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        //通过集合的比较其来实现排序
        Collections.sort(list,new Comparator<Map.Entry<String, Integer>>() {

            @Override
            public int compare(Entry<String, Integer> o1,
                    Entry<String, Integer> o2) {
                // TODO Auto-generated method stub
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        for (Entry<String, Integer> entry : list) {
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值