2013-11-09 63 views
1

我已定義爲一個HashMap如下在一個HashMap排序基於的ArrayList <String>的大小

Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); 

我然後存儲從DATABSE數據在此HashMap和顯示內容在控制檯上如下,其中朝向所述條目留下的--->是ID和向右項是由該ID

165767--->[dual-boot, windows, uninstall, ati, graphics, multiple-monitors] 
6873 --->[kubuntu, re-installation] 
34228--->[11.10, unity, launcher, libreoffice, icons] 

我想在下降的基礎上,他們已經使用即基於地圖標籤數順序編號的排序中使用的標籤。獲得(key).size()以便輸出應該是ID 165767,然後是34228,然後是6873等等。

我試着用TreeMap這樣做,但我無法弄清楚如何根據大小而不是按鍵的值,而且按降序排列。

+0

[HashMap的排序由值]的可能重複(http://stackoverflow.com/questions/8119366/sorting-hashmap-by-values)。另外,您需要一個比較器,根據它們的長度比較列表。 –

+0

您可以先製作地圖,然後再進行排序嗎? –

+0

或者...如何獲得已經從數據庫中排序的數據,您可以簡單地將其轉換爲HashMap?你應該可以用SQL來做到這一點。 – scottb

回答

0

編輯

我的輸出應該基於標籤數量和 ID

Map<String, ArrayList<String>> map = new TreeMap<String, ArrayList<String>>(); 


map.put("165767",new ArrayList<String>(Arrays.asList("dual-boot", "dual-boot", "windows", "uninstall", "ati", "graphics", "multiple-monitors"))); 
map.put("6873",new ArrayList<String>(Arrays.asList("kubuntu", "kubuntu", "re-installation"))); 
map.put("0000000000000000",new ArrayList<String>(Arrays.asList("test","test", "test"))); 
map.put("0125",new ArrayList<String>(Arrays.asList("dual-boot", "windows", "uninstall", "ati", "graphics", "multiple-monitors"))); 


for(ArrayList<String> l : map.values()){ 
    Set<String> hs = new HashSet<>(); 
    hs.addAll(l); 
    l.clear(); 
    l.addAll(hs); 
} 

List<ArrayList<String>> l = new ArrayList<>(map.values()); 
Collections.sort(l, new Comparator<ArrayList<String>>(){ 
    public int compare(ArrayList<String> s1, ArrayList<String> s2){ 
     return Integer.compare(s2.size(), s1.size());     
    }}); 

for(ArrayList<String> a : l){ 
    Iterator<Entry<String, ArrayList<String>>> iter = map.entrySet().iterator(); 
    while (iter.hasNext()) { 
     Entry<String, ArrayList<String>> e = iter.next(); 
     if(e.getValue().equals(a)){ 

      System.out.println(e.getKey() + "-" + a); 
      iter.remove(); 
     } 
    } 
} 

輸出的不是大小進行排序:

0125-[uninstall, dual-boot, graphics, windows, ati, multiple-monitors] 
165767-[uninstall, dual-boot, graphics, windows, ati, multiple-monitors] 
6873-[re-installation, kubuntu] 
0000000000000000-[test] 
+0

我認爲你誤解了這個問題。我的輸出應該根據標籤的數量而不是ID的大小排序。你的輸出看起來像是根據ID的大小進行排序。 – user2916886

+0

@ user2916886檢查我的編輯。這是你在找什麼? –

+0

是的,這是我一直在尋找。謝謝你的幫助。只是更喜歡。假設我的標籤有可能重複。如何將它們從最終顯示中刪除? – user2916886

3

這創建了一個排序的ID列表。

List<String> sortedIds = new ArrayList<String>(map.getKeys()); 
Collections.sort(sortedIds, new Comparator<String>() { 
    public int compare(String a, String b) { 
     return map.get(b).size() - map.get(a).size(); 
    } 
}); 

不是說你永遠不會保持SortedMap(如TreeMap),上排序可變值(如一個ArrayList的長度)。由於排序順序用於查找值,因此如果"id123"變得大於"id456"而沒有收集知道它,則可能會導致非常大的問題。

0

我有類似的情況和此代碼爲我工作(有時我必須空):

 private static Map<Object,List<Object>> sortByArraySizeDesc(Map<Object,List<Object>> map) { 
     List<List<Object>> list = new LinkedList(map.entrySet()); 
     Collections.sort(list, new Comparator() { 
       public int compare(Object o1, Object o2) { 
        if (o1 == null && o2 == null) { return 0; } 
        else if (o1 == null) { return 1;} 
        else if (o2 == null) { return -1; } 
        int size1 = ((List) ((Map.Entry) (o1)).getValue()).size(); 
        int size2 = ((List) ((Map.Entry) (o2)).getValue()).size(); 
        return size2 - size1; 
       } 
     }); 

     Map res = new LinkedHashMap(); 
     for (Iterator it = list.iterator(); it.hasNext();) { 
      Map.Entry entry = (Map.Entry)it.next(); 
      res.put(entry.getKey(), entry.getValue()); 
     } 
     return res; 
    } 
相關問題