Java 使用 lambdas 和流按值对地图进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29567575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:13:35  来源:igfitidea点击:

Sort map by value using lambdas and streams

javajava-8java-stream

提问by Sudarsana Kasireddy

I'm new to Java 8, not sure how to use streams and it's methods to sort. If I have map as below, how to sort this map by value to take only top 10 entries using Java 8.

我是 Java 8 的新手,不确定如何使用流及其排序方法。如果我有如下地图,如何使用 Java 8 按值对该地图进行排序以仅获取前 10 个条目。

HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 10);
        map.put("b", 30);
        map.put("c", 50);
        map.put("d", 40);
        map.put("e", 100);
        map.put("f", 60);
        map.put("g", 110);
        map.put("h", 50);
        map.put("i", 90);
        map.put("k", 70);
        map.put("L", 80);

I know before Java 8, we can sort as this link: https://stackoverflow.com/a/109389/4315608

我知道在 Java 8 之前,我们可以按这个链接排序:https: //stackoverflow.com/a/109389/4315608

采纳答案by ericbn

You can always start reading the documentationand sometutorials.

您可以随时开始阅读文档一些教程

map.entrySet().stream()
        .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) 
        .limit(10) 
        .forEach(System.out::println); // or any other terminal method

Reference

参考

http://www.leveluplunch.com/java/examples/sort-order-map-by-values/

http://www.leveluplunch.com/java/examples/sort-order-map-by-values/

回答by pomo

List<Map.Entry<String, Integer>> entries = newArrayList(map.entrySet());
Collections.sort(entries, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));
List<Map.Entry<String, Integer>> top10 = entries.subList(0, Math.min(entries.size(), 10));

回答by Ajay Kumar

See the stack flow thread hereand example here

请参阅此处的堆栈流线程和此处的示例

Map<String,Person> map = new HashMap<>();
map.put("g",new Person(5, "EE", 51, Person.SEX.FEMALE, "A"));
map.put("a",new Person(4, "DD", 25, Person.SEX.MALE, "D"));
map.put("e",new Person(3, "CC", 44, Person.SEX.FEMALE,"B"));

Map<String,Person> sortedNewMap = map.entrySet().stream().sorted((e1,e2)->
        e1.getValue().getLocation().compareTo(e2.getValue().getLocation()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                (e1, e2) -> e1, LinkedHashMap::new));
sortedNewMap.forEach((key,val)->{
    System.out.println(key+ " = "+ val.toString());
});

回答by Nycrera

If you want sort by integer or float value of value object

如果要按值对象的整数或浮点值排序

Map<String,Person> map = new HashMap<>();
map.put("g",new Person(5, "EE", 51, Person.SEX.FEMALE, "A"));
map.put("a",new Person(4, "DD", 25, Person.SEX.MALE, "D"));
map.put("e",new Person(3, "CC", 44, Person.SEX.FEMALE,"B"));

You can use,

您可以使用,

Map<String,Person> sortedNewMap = map.entrySet().stream().sorted((e1,e2)->
        Integer.compare(e1.getValue().getAge(), e2.getValue().getAge()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                (e1, e2) -> e1, LinkedHashMap::new));