zoukankan      html  css  js  c++  java
  • Java8-对map排序

    1、Java8对map按key排序

    /**
     * @author : fengkun
     * @date : 19-3-10
     * 内容 : Java8对map按key排序
     */
    public class SortByKeyExample {
    
        public static void main(String[] args) {
            Map<String, Integer> unsortMap = new HashMap<>();
            unsortMap.put("z", 10);
            unsortMap.put("b", 5);
            unsortMap.put("a", 6);
            unsortMap.put("c", 20);
            unsortMap.put("d", 1);
            unsortMap.put("e", 7);
            unsortMap.put("y", 8);
            unsortMap.put("n", 99);
            unsortMap.put("g", 50);
            unsortMap.put("m", 2);
            unsortMap.put("f", 9);
            System.out.println(unsortMap);
    
            Map<String, Integer> result1 = unsortMap.entrySet().stream()
                    .sorted(Map.Entry.comparingByKey())
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                            (oleValue, newValue) -> oleValue, LinkedHashMap::new));
            System.out.println(result1);
    
            Map<String, Integer> result2 = new LinkedHashMap<>();
            unsortMap.entrySet().stream()
                    .sorted(Map.Entry.comparingByKey())
                    .forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));
            System.out.println(result2);
        }
    }

    2、Java8对map按Vaule排序

    /**
     * @author : fengkun
     * @date : 19-3-10
     * 内容 : Java8对map按Vaule排序
     */
    public class SoryByKeyExample {
    
        public static void main(String[] args) {
            Map<String, Integer> unsortMap = new HashMap<>();
            unsortMap.put("z", 10);
            unsortMap.put("b", 5);
            unsortMap.put("a", 6);
            unsortMap.put("c", 20);
            unsortMap.put("d", 1);
            unsortMap.put("e", 7);
            unsortMap.put("y", 8);
            unsortMap.put("n", 99);
            unsortMap.put("g", 50);
            unsortMap.put("m", 2);
            unsortMap.put("f", 9);
            System.out.println(unsortMap);
    
            Map<String, Integer> result1 = unsortMap.entrySet().stream()
                    .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                            (oldValue, newValue) -> oldValue, LinkedHashMap::new));
    
            System.out.println(result1);
    
            Map<String, Integer> result2 = new LinkedHashMap<>();
            unsortMap.entrySet().stream()
                    .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                    .forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));
            System.out.println(result2);
        }
    }
  • 相关阅读:
    [CF528D] Fuzzy Search
    [WC2013] 糖果公园
    [APIO2011] 方格染色
    [CTSC2017] 吉夫特
    [HNOI/AHOI2018] 转盘
    [CTSC2008] 网络管理
    [HAOI2018] 苹果树
    [SCOI2016] 萌萌哒
    git创建分支并提交项目
    git 常规操作
  • 原文地址:https://www.cnblogs.com/fengkunangel/p/10507916.html
Copyright © 2011-2022 走看看