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());
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u012485480/article/details/80200105