原文: https://beginnersbook.com/2013/12/how-to-sort-hashmap-in-java-by-keys-and-values/

我们知道HashMap默认不保留任何顺序。如果有需要,我们需要根据需求明确排序。在本教程中,我们将学习如何对TreeMap使用Comparator按键和按值进行排序。

HashMap按键排序

在这个例子中,我们使用TreeMap集合类基于键对HashMap进行排序。

  1. package beginnersbook.com;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.TreeMap;
  5. import java.util.Set;
  6. import java.util.Iterator;
  7. public class Details {
  8. public static void main(String[] args) {
  9. HashMap<Integer, String> hmap = new HashMap<Integer, String>();
  10. hmap.put(5, "A");
  11. hmap.put(11, "C");
  12. hmap.put(4, "Z");
  13. hmap.put(77, "Y");
  14. hmap.put(9, "P");
  15. hmap.put(66, "Q");
  16. hmap.put(0, "R");
  17. System.out.println("Before Sorting:");
  18. Set set = hmap.entrySet();
  19. Iterator iterator = set.iterator();
  20. while(iterator.hasNext()) {
  21. Map.Entry me = (Map.Entry)iterator.next();
  22. System.out.print(me.getKey() + ": ");
  23. System.out.println(me.getValue());
  24. }
  25. Map<Integer, String> map = new TreeMap<Integer, String>(hmap);
  26. System.out.println("After Sorting:");
  27. Set set2 = map.entrySet();
  28. Iterator iterator2 = set2.iterator();
  29. while(iterator2.hasNext()) {
  30. Map.Entry me2 = (Map.Entry)iterator2.next();
  31. System.out.print(me2.getKey() + ": ");
  32. System.out.println(me2.getValue());
  33. }
  34. }
  35. }

输出:

  1. Before Sorting:
  2. 0: R
  3. 4: Z
  4. 5: A
  5. 66: Q
  6. 9: P
  7. 77: Y
  8. 11: C
  9. After Sorting:
  10. 0: R
  11. 4: Z
  12. 5: A
  13. 9: P
  14. 11: C
  15. 66: Q
  16. 77: Y

HashMap按值排序

在这个例子中,我们使用Comparator按值对HashMap进行排序。

  1. package beginnersbook.com;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.LinkedHashMap;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Set;
  11. public class HMapSortingByvalues {
  12. public static void main(String[] args) {
  13. HashMap<Integer, String> hmap = new HashMap<Integer, String>();
  14. hmap.put(5, "A");
  15. hmap.put(11, "C");
  16. hmap.put(4, "Z");
  17. hmap.put(77, "Y");
  18. hmap.put(9, "P");
  19. hmap.put(66, "Q");
  20. hmap.put(0, "R");
  21. System.out.println("Before Sorting:");
  22. Set set = hmap.entrySet();
  23. Iterator iterator = set.iterator();
  24. while(iterator.hasNext()) {
  25. Map.Entry me = (Map.Entry)iterator.next();
  26. System.out.print(me.getKey() + ": ");
  27. System.out.println(me.getValue());
  28. }
  29. Map<Integer, String> map = sortByValues(hmap);
  30. System.out.println("After Sorting:");
  31. Set set2 = map.entrySet();
  32. Iterator iterator2 = set2.iterator();
  33. while(iterator2.hasNext()) {
  34. Map.Entry me2 = (Map.Entry)iterator2.next();
  35. System.out.print(me2.getKey() + ": ");
  36. System.out.println(me2.getValue());
  37. }
  38. }
  39. private static HashMap sortByValues(HashMap map) {
  40. List list = new LinkedList(map.entrySet());
  41. // Defined Custom Comparator here
  42. Collections.sort(list, new Comparator() {
  43. public int compare(Object o1, Object o2) {
  44. return ((Comparable) ((Map.Entry) (o1)).getValue())
  45. .compareTo(((Map.Entry) (o2)).getValue());
  46. }
  47. });
  48. // Here I am copying the sorted list in HashMap
  49. // using LinkedHashMap to preserve the insertion order
  50. HashMap sortedHashMap = new LinkedHashMap();
  51. for (Iterator it = list.iterator(); it.hasNext();) {
  52. Map.Entry entry = (Map.Entry) it.next();
  53. sortedHashMap.put(entry.getKey(), entry.getValue());
  54. }
  55. return sortedHashMap;
  56. }
  57. }

输出:

  1. Before Sorting:
  2. 0: R
  3. 4: Z
  4. 5: A
  5. 66: Q
  6. 9: P
  7. 77: Y
  8. 11: C
  9. After Sorting:
  10. 5: A
  11. 11: C
  12. 9: P
  13. 66: Q
  14. 0: R
  15. 77: Y
  16. 4: Z

参考: