Java8 对Map(key/value)排序后取TopN

import com.alibaba.fastjson.JSON;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) {
        Map<String,Integer> mapRepeat = new HashMap<>();
        mapRepeat.put("aa", 1);
        mapRepeat.put("bb", 45);
        mapRepeat.put("cc", 32);
        mapRepeat.put("dd", 226);
        mapRepeat.put("ee", 16);
        mapRepeat.put("ff", 320);
        mapRepeat.put("gg", 99);

        // 1.8以后 使用lambda表达式和Stream处理
        // 1.对Map的value进行降序排序,并取前5个key
        List<String> mobileList = mapRepeat.entrySet().stream()
                .sorted((Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) -> e2.getValue() - e1.getValue())
                .map(entry -> entry.getKey()).collect(Collectors.toList())
                .subList(0, 5);
        System.out.println(JSON.toJSONString(mobileList));


        // 2、正向对Map的value排序并将结果输出到LinkedHashMap
        final Map<String, Integer> sortedByCount1 = mapRepeat.entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        System.out.println(JSON.toJSONString(sortedByCount1));

        // 3、反向 reversed对Map的value排序并将结果输出到LinkedHashMap
        final Map<String, Integer> sortedByCount2 = mapRepeat.entrySet()
                .stream()
                .sorted((Map.Entry.<String, Integer>comparingByValue().reversed()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        System.out.println(JSON.toJSONString(sortedByCount2));


        // 4.正向 Comparator 对Map的value排序并将结果输出到LinkedHashMap
        final Map<String, Integer> sortedByCount3 = mapRepeat.entrySet()
                .stream()
                .sorted((e1, e2) -> e1.getValue().compareTo(e2.getValue()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        System.out.println(JSON.toJSONString(sortedByCount3));


        //反向 Comparator 对Map的value排序并将结果输出到LinkedHashMap
        final Map<String, Integer> sortedByCount4 = mapRepeat.entrySet()
                .stream()
                .sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        System.out.println(JSON.toJSONString(sortedByCount4));

        // 1.8以前
        List<Map.Entry<String, Integer>> list1 = new ArrayList<>();
        list1.addAll(mapRepeat.entrySet());
        Collections.sort(list1, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue() - o2.getValue();
            }
        });
        for (Map.Entry<String, Integer> entry : list1) {
            System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
        }
    }
}

 

    原文作者:天~嘿
    原文地址: https://blog.csdn.net/weixin_42231507/article/details/91466926
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞