我有一個同時包含鍵和值的字符串的Map。 java
數據以下: app
「 question1」,「 1」
「 question9」,「 1」
「 question2」,「 4」
「 question5」,「 2」 函數
我想根據其鍵對地圖進行排序。 所以,最後,我將看到question1, question2, question3
...等。 this
最終,我試圖從該Map中獲取兩個字符串。 spa
如今,我有如下內容: code
Iterator it = paramMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); questionAnswers += pairs.getKey() + ","; }
這使個人問題成串出現,但順序不正確。 排序
若是您已經有了地圖,而且想按鍵對它進行排序,則只需使用: three
Map<String, String> treeMap = new TreeMap<String, String>(yourMap);
一個完整的工做示例: 字符串
import java.util.HashMap; import java.util.Set; import java.util.Map; import java.util.TreeMap; import java.util.Iterator; class SortOnKey { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<String,String>(); hm.put("3","three"); hm.put("1","one"); hm.put("4","four"); hm.put("2","two"); printMap(hm); Map<String, String> treeMap = new TreeMap<String, String>(hm); printMap(treeMap); }//main public static void printMap(Map<String,String> map) { Set s = map.entrySet(); Iterator it = s.iterator(); while ( it.hasNext() ) { Map.Entry entry = (Map.Entry) it.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println(key + " => " + value); }//while System.out.println("========================"); }//printMap }//class
只需使用TreeMap get
new TreeMap<String, String>(unsortMap);
請注意,TreeMap是根據其「鍵」的天然順序排序的
此代碼能夠按升序和降序這兩個順序對鍵值映射進行排序。
<K, V extends Comparable<V>> Map<K, V> sortByValues (final Map<K, V> map, int ascending) { Comparator<K> valueComparator = new Comparator<K>() { private int ascending; public int compare(K k1, K k2) { int compare = map.get(k2).compareTo(map.get(k1)); if (compare == 0) return 1; else return ascending*compare; } public Comparator<K> setParam(int ascending) { this.ascending = ascending; return this; } }.setParam(ascending); Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator); sortedByValues.putAll(map); return sortedByValues; }
舉個例子:
Map<Integer,Double> recommWarrVals = new HashMap<Integer,Double>(); recommWarrVals = sortByValues(recommWarrVals, 1); // Ascending order recommWarrVals = sortByValues(recommWarrVals,-1); // Descending order
若是您不能使用TreeMap
,那麼在Java 8中,咱們能夠使用Collectors
的toMap()方法,該方法具備如下參數:
Java 8示例
Map<String,String> sample = new HashMap<>(); // push some values to map Map<String, String> newMapSortedByKey = sample.entrySet().stream() .sorted(Map.Entry.<String,String>comparingByKey().reversed()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); Map<String, String> newMapSortedByValue = sample.entrySet().stream() .sorted(Map.Entry.<String,String>comparingByValue().reversed()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1,e2) -> e1, LinkedHashMap::new));
咱們能夠修改該示例以使用自定義比較器並基於如下項對鍵進行排序:
Map<String, String> newMapSortedByKey = sample.entrySet().stream() .sorted((e1,e2) -> e1.getKey().compareTo(e2.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1,e2) -> e1, LinkedHashMap::new));
在Java 8中,您還能夠使用.stream()。sorted():
myMap.keySet().stream().sorted().forEach(key -> { String value = myMap.get(key); System.out.println("key: " + key); System.out.println("value: " + value); } );