Java 8 –如何对地图排序

电玩女神 2023-02-15 05:07 91阅读 0赞

分类分类

Java 8 Stream示例,用于通过键或值对Map进行排序。

1.快速说明

在Java 8中对地图进行排序的步骤。

  1. 将地图转换成流
  2. 解决
  3. 收集并返回一个新的LinkedHashMap (保留订单)

    Map result = map.entrySet().stream()

    1. .sorted(Map.Entry.comparingByKey())
    2. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
    3. (oldValue, newValue) -> oldValue, LinkedHashMap::new));

PS默认情况下, Collectors.toMap将返回一个HashMap

2.按键排序

SortByKeyExample.java

  1. package com.mkyong.test;
  2. import java.util.HashMap;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.stream.Collectors;
  6. public class SortByKeyExample {
  7. public static void main(String[] argv) {
  8. Map<String, Integer> unsortMap = new HashMap<>();
  9. unsortMap.put("z", 10);
  10. unsortMap.put("b", 5);
  11. unsortMap.put("a", 6);
  12. unsortMap.put("c", 20);
  13. unsortMap.put("d", 1);
  14. unsortMap.put("e", 7);
  15. unsortMap.put("y", 8);
  16. unsortMap.put("n", 99);
  17. unsortMap.put("g", 50);
  18. unsortMap.put("m", 2);
  19. unsortMap.put("f", 9);
  20. System.out.println("Original...");
  21. System.out.println(unsortMap);
  22. // sort by keys, a,b,c..., and return a new LinkedHashMap
  23. // toMap() will returns HashMap by default, we need LinkedHashMap to keep the order.
  24. Map<String, Integer> result = unsortMap.entrySet().stream()
  25. .sorted(Map.Entry.comparingByKey())
  26. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
  27. (oldValue, newValue) -> oldValue, LinkedHashMap::new));
  28. // Not Recommend, but it works.
  29. //Alternative way to sort a Map by keys, and put it into the "result" map
  30. Map<String, Integer> result2 = new LinkedHashMap<>();
  31. unsortMap.entrySet().stream()
  32. .sorted(Map.Entry.comparingByKey())
  33. .forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));
  34. System.out.println("Sorted...");
  35. System.out.println(result);
  36. System.out.println(result2);
  37. }
  38. }

输出量

  1. Original...
  2. {a=6, b=5, c=20, d=1, e=7, f=9, g=50, y=8, z=10, m=2, n=99}
  3. Sorted...
  4. {a=6, b=5, c=20, d=1, e=7, f=9, g=50, m=2, n=99, y=8, z=10}
  5. {a=6, b=5, c=20, d=1, e=7, f=9, g=50, m=2, n=99, y=8, z=10}

3.按值排序

SortByValueExample.java

  1. package com.mkyong.test;
  2. package com.mkyong;
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.LinkedHashMap;
  6. import java.util.Map;
  7. import java.util.stream.Collectors;
  8. public class SortByValueExample {
  9. public static void main(String[] argv) {
  10. Map<String, Integer> unsortMap = new HashMap<>();
  11. unsortMap.put("z", 10);
  12. unsortMap.put("b", 5);
  13. unsortMap.put("a", 6);
  14. unsortMap.put("c", 20);
  15. unsortMap.put("d", 1);
  16. unsortMap.put("e", 7);
  17. unsortMap.put("y", 8);
  18. unsortMap.put("n", 99);
  19. unsortMap.put("g", 50);
  20. unsortMap.put("m", 2);
  21. unsortMap.put("f", 9);
  22. System.out.println("Original...");
  23. System.out.println(unsortMap);
  24. //sort by values, and reserve it, 10,9,8,7,6...
  25. Map<String, Integer> result = unsortMap.entrySet().stream()
  26. .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
  27. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
  28. (oldValue, newValue) -> oldValue, LinkedHashMap::new));
  29. //Alternative way
  30. Map<String, Integer> result2 = new LinkedHashMap<>();
  31. unsortMap.entrySet().stream()
  32. .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
  33. .forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));
  34. System.out.println("Sorted...");
  35. System.out.println(result);
  36. System.out.println(result2);
  37. }
  38. }

输出量

  1. Original...
  2. {a=6, b=5, c=20, d=1, e=7, f=9, g=50, y=8, z=10, m=2, n=99}
  3. Sorted...
  4. {n=99, g=50, c=20, z=10, f=9, y=8, e=7, a=6, b=5, m=2, d=1}
  5. {n=99, g=50, c=20, z=10, f=9, y=8, e=7, a=6, b=5, m=2, d=1}

4. Map <对象,对象>

Stream无法直接对Map<Object,Object>排序。 要解决该问题,请将其转换为Map<String,String> ,请查看以下示例。

注意
如果您有更好的想法进行排序,请告诉我。

DisplayApp.java

  1. package com.mkyong;
  2. import java.util.LinkedHashMap;
  3. import java.util.Map;
  4. import java.util.Properties;
  5. import java.util.Set;
  6. import java.util.stream.Collectors;
  7. public class DisplayApp {
  8. public static void main(String[] args) {
  9. Properties properties = System.getProperties();
  10. // not easy to sort this
  11. Set<Map.Entry<Object, Object>> entries = properties.entrySet();
  12. LinkedHashMap<String, String> collect = entries.stream()
  13. //Map<String, String>
  14. .collect(Collectors.toMap(k -> (String) k.getKey(), e -> (String) e.getValue()))
  15. .entrySet()
  16. .stream()
  17. .sorted(Map.Entry.comparingByKey())
  18. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
  19. (oldValue, newValue) -> oldValue, LinkedHashMap::new));
  20. collect.forEach((k, v) -> System.out.println(k + ":" + v));
  21. }
  22. }

注意
不使用Java 8? 尝试使用这种经典方法在Java中对Map进行排序 。

参考文献

  1. 如何用Java对地图排序
  2. LinkedHashMap JavaDoc
  3. Collectors.toMap()JavaDoc
  4. 通配符带来更多乐趣
  5. Collections.sort()签名
  6. Java中超级T>和扩展T>之间的区别

标记: 通用 java8 地图 排序 排序 流

翻译自: https://mkyong.com/java8/java-8-how-to-sort-a-map/

发表评论

表情:
评论列表 (有 0 条评论,91人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java如何对象排序

    前言: 我们知道,在平时做项目的过程中,我们总会用到各种各样的排序,或是升序,或是降序。在java中,要实现排序有好多中方式,比如我们耳熟能详的冒泡排序、选择排序等,但是我们