企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 如何按键和值对 Java 中的`HashMap`进行排序 > 原文: [https://beginnersbook.com/2013/12/how-to-sort-hashmap-in-java-by-keys-and-values/](https://beginnersbook.com/2013/12/how-to-sort-hashmap-in-java-by-keys-and-values/) 我们知道`HashMap`默认不保留任何顺序。如果有需要,我们需要根据需求明确排序。在本教程中,我们将学习如何对`TreeMap`使用`Comparator`按键和按值进行排序。 ## `HashMap`按键排序 在这个例子中,我们使用`TreeMap`集合类基于键对`HashMap`进行排序。 ```java package beginnersbook.com; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import java.util.Set; import java.util.Iterator; public class Details { public static void main(String[] args) { HashMap<Integer, String> hmap = new HashMap<Integer, String>(); hmap.put(5, "A"); hmap.put(11, "C"); hmap.put(4, "Z"); hmap.put(77, "Y"); hmap.put(9, "P"); hmap.put(66, "Q"); hmap.put(0, "R"); System.out.println("Before Sorting:"); Set set = hmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry me = (Map.Entry)iterator.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } Map<Integer, String> map = new TreeMap<Integer, String>(hmap); System.out.println("After Sorting:"); Set set2 = map.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry me2 = (Map.Entry)iterator2.next(); System.out.print(me2.getKey() + ": "); System.out.println(me2.getValue()); } } } ``` 输出: ```java Before Sorting: 0: R 4: Z 5: A 66: Q 9: P 77: Y 11: C After Sorting: 0: R 4: Z 5: A 9: P 11: C 66: Q 77: Y ``` ## `HashMap`按值排序 在这个例子中,我们使用`Comparator`按值对`HashMap`进行排序。 ```java package beginnersbook.com; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; public class HMapSortingByvalues { public static void main(String[] args) { HashMap<Integer, String> hmap = new HashMap<Integer, String>(); hmap.put(5, "A"); hmap.put(11, "C"); hmap.put(4, "Z"); hmap.put(77, "Y"); hmap.put(9, "P"); hmap.put(66, "Q"); hmap.put(0, "R"); System.out.println("Before Sorting:"); Set set = hmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry me = (Map.Entry)iterator.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } Map<Integer, String> map = sortByValues(hmap); System.out.println("After Sorting:"); Set set2 = map.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry me2 = (Map.Entry)iterator2.next(); System.out.print(me2.getKey() + ": "); System.out.println(me2.getValue()); } } private static HashMap sortByValues(HashMap map) { List list = new LinkedList(map.entrySet()); // Defined Custom Comparator here Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getValue()) .compareTo(((Map.Entry) (o2)).getValue()); } }); // Here I am copying the sorted list in HashMap // using LinkedHashMap to preserve the insertion order HashMap sortedHashMap = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); sortedHashMap.put(entry.getKey(), entry.getValue()); } return sortedHashMap; } } ``` 输出: ```java Before Sorting: 0: R 4: Z 5: A 66: Q 9: P 77: Y 11: C After Sorting: 5: A 11: C 9: P 66: Q 0: R 77: Y 4: Z ``` #### 参考: * [`HashMap` Javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html) * [`Comparator` 文档](https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html) * [`LinkedHashMap` Javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html) * [`TreeMap` 文档](https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html)