java8中对Map集合排序

java8中对Map集合排序,利用Stream函数,结合lamda表达式方便操作

  public static void main(String[] args)
    {
        //在java8中,利用lamda表达式对map进行排序
        HashMap  map =  new HashMap();
        map.put(8,2);
        map.put(5,6);
        map.put(6,5);
        map.put(3,7);
        map.put(4,8);
        map.put(7,3);
        map.put(2,1);
        map.put(1,4);
        map.forEach((k, v) -> System.out.println("原始Map数据:"+"key:value = " + k + ":" + v));
        //按key值升序排列
        HashMap result0 = map.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        result0.forEach((k, v) -> System.out.println("按key升序:"+"key:value = " + k + ":" + v));

        //按key值降序排列
        HashMap result1 = map.entrySet().stream()
                .sorted(Map.Entry.comparingByKey().reversed())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        result1.forEach((k, v) -> System.out.println("按key降序排列:"+"key:value = " + k + ":" + v));

        //按value值升序排列
        HashMap result2 = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        result2.forEach((k, v) -> System.out.println("按Value升序:"+"key:value = " + k + ":" + v));
        //按value值降序排列
        HashMap result3 = map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue().reversed())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        result3.forEach((k, v) -> System.out.println("按Value将序结果:"+"key:value = " + k + ":" + v));
    }

运行结果:
原始Map数据:key:value = 1:4
原始Map数据:key:value = 2:1
原始Map数据:key:value = 3:7
原始Map数据:key:value = 4:8
原始Map数据:key:value = 5:6
原始Map数据:key:value = 6:5
原始Map数据:key:value = 7:3
原始Map数据:key:value = 8:2
按key升序:key:value = 1:4
按key升序:key:value = 2:1
按key升序:key:value = 3:7
按key升序:key:value = 4:8
按key升序:key:value = 5:6
按key升序:key:value = 6:5
按key升序:key:value = 7:3
按key升序:key:value = 8:2
按key降序排列:key:value = 8:2
按key降序排列:key:value = 7:3
按key降序排列:key:value = 6:5
按key降序排列:key:value = 5:6
按key降序排列:key:value = 4:8
按key降序排列:key:value = 3:7
按key降序排列:key:value = 2:1
按key降序排列:key:value = 1:4
按Value升序:key:value = 2:1
按Value升序:key:value = 8:2
按Value升序:key:value = 7:3
按Value升序:key:value = 1:4
按Value升序:key:value = 6:5
按Value升序:key:value = 5:6
按Value升序:key:value = 3:7
按Value升序:key:value = 4:8
按Value将序结果:key:value = 4:8
按Value将序结果:key:value = 3:7
按Value将序结果:key:value = 5:6
按Value将序结果:key:value = 6:5
按Value将序结果:key:value = 1:4
按Value将序结果:key:value = 7:3
按Value将序结果:key:value = 8:2
按Value将序结果:key:value = 2:1

你可能感兴趣的:(java,java,scala,rust)