2015-07-13 54 views
0

樹圖嗎?實際上樹型圖本身可以對鍵進行排序,但我想對鍵和值也進行排序。如何使用鍵值對TreeMap進行排序

TreeMap <Double, List<String>> treemap = new TreeMap <Double, List<String>>(); 

Example

Keys : 1.84, 2.35, 5.89, 0.21 
values: {Burger, 02058795247}, {Pizza, 02087958742}, {Rolls, 020547896874}, {Sandwich, 02058967412} 

結果應該是

keys : 0.21 
Values: {Sandwich, 02058967412} 
keys : 0.21, 1.84 
Values: {Sandwich, 02058967412}, {Burger, 02058795247} 
keys : 0.21, 1.84, 2.35 
Values: {Sandwich, 02058967412}, {Burger, 02058795247}, {Pizza, 02087958742} 
keys : 0.21, 1.84, 2.35, 5.89 
Values: {Sandwich, 02058967412}, {Burger, 02058795247}, {Pizza, 02087958742}, {Rolls, 020547896874} 

但我得到類似結果

keys : 0.21  
    values: {Burger, 02058795247} 
    Key: 0.21, 1.84 
    Value : {Burger, 02058795247, Pizza, 02087958742} 
    keys : 0.21, 1.84, 2.35 
    Value: {Burger, 02058795247, Pizza, 02087958742, Rolls, 020547896874} 
    keys : 0.21, 1.84, 2.35, 5.89 
    Value :{Burger, 02058795247, Pizza, 02087958742, Rolls, 020547896874, Sandwich, 02058967412} 
+0

是你可以對它進行排序....我*會很快發佈了答案。 – RajSharma

+0

可能重複[TreeMap按值排序](http://stackoverflow.com/questions/2864840/treemap-sort-by-value) – John

+0

由於我使用列表作爲值,因此無法從此給定鏈接獲得解決方案。那麼如何使用比較器來獲得價值呢? @ user3360241 – abc

回答

0

排序之後。

獲取鍵值條目列表,並使用自定義比較器對其進行排序。不幸的是,這個值是一串字符串,也許SortedSet<String>可能更容易。

List<Map.Entry<Double, List<String>>> entries = new ArrayList<>(treemap.entrySet()); 
Collections.sort(entries, new Comparator<Map.Entry<Double, List<String>>>() { 
    @Override 
    int compareTo(Map.Entry<Double, List<String>> lhs, 
      Map.Entry<Double, List<String>> rhs) { 
     int cmp = Double.compare(lhs.getKey(), rhs.getKey()); 
     if (cmp == 0) { 
      Iterator<String> lit = lhs.getValue().iterator(); 
      Iterator<String> rit = rhs.getValue().iterator(); 
      while (cmp == 0) { 
       boolean lhas = lit.hasNext(); 
       boolean rhas = rit.hasNext(); 
       if (!lhas && !rhas) { 
        break; 
       } 
       if (!lhas) { 
        cmp = -1; 
       } else if (!rhas) { 
        cmp = 1; 
       } else { 
        cmp == lit.next().compareTo(rit.next()); 
       } 
      } 
     } 
     return cmp; 
    }); 

爲(Map.Entry的>項:項){ ...進入 }

也許List<String>本身應該是一個SortedSet<String>,一個TreeSet<String>

+0

plz舉例說明它的工作方式? – abc

0

您可以使用此方法:

private static Map<Double, List<String>> sortByComparator(Map<Double, List<String>> unsortMap) 
    { 

     List<Map.Entry<Double, List<String>>> list = new LinkedList<>(unsortMap.entrySet()); 

     // Sorting the list based on values 
     Collections.sort(list, new Comparator<Map.Entry<Double, List<String>>>() 
     { 
      public int compare(Map.Entry<Double, List<String>> o1, 
           Map.Entry<Double, List<String>> o2) 
      { 
       if(o1.getKey() == o2.getKey()) return 0; 
       return (o1.getKey() < o2.getKey() == true ? -1 : 1); 
      } 
     }); 

     // Maintaining insertion order with the help of LinkedList 
     Map<Double, List<String>> sortedMap = new LinkedHashMap<>(); 
     for (Map.Entry<Double, List<String>> entry : list) 
     { 
      sortedMap.put(entry.getKey(), entry.getValue()); 
     } 

     return sortedMap; 
    } 
+0

謝謝,但我已經使用它不適合我的條件@KinnarVasa – abc

+0

看來,你還沒有嘗試運行上面的代碼,請做一次嘗試並檢查輸出。 –

+0

不用@KinnarVasa我已經試過這個代碼那就是爲什麼我說.. – abc

0

這裏是整個代碼:

public class InformationList extends Activity 
{ 
    TreeMap<Double, List<String>> treemap = new TreeMap <Double, List<String>>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     List<String> list = new ArrayList<>(); 
     list.add("Bruger"); 
     list.add("234234234"); 

     treemap.put(1.84, list); 

     List<String> list1 = new ArrayList<>(); 
     list1.add("Pizza"); 
     list1.add("342342"); 
     treemap.put(2.35, list1); 

     List<String> list2 = new ArrayList<>(); 
     list2.add("Rolls"); 
     list2.add("453453"); 
     treemap.put(5.89, list2); 

     List<String> list3 = new ArrayList<>(); 
     list3.add("Swandwitch"); 
     list3.add("756334"); 
     treemap.put(0.21, list3); 

     Map<Double, List<String>> sortList = sortByComparator(treemap); 
     Log.e("Sort Item :", "Sort List: "+ sortList.toString()); 

    } 

    private static Map<Double, List<String>> sortByComparator(Map<Double, List<String>> unsortMap) 
    { 

     List<Map.Entry<Double, List<String>>> list = new LinkedList<>(unsortMap.entrySet()); 

     // Sorting the list based on values 
     Collections.sort(list, new Comparator<Map.Entry<Double,List<String>>>() { 
      public int compare(Map.Entry<Double, List<String>> o1, 
           Map.Entry<Double, List<String>> o2) { 
       if (o1.getKey() == o2.getKey()) return 0; 
       return (o1.getKey() < o2.getKey() == true ? -1 : 1); 
      } 
     }); 

     // Maintaining insertion order with the help of LinkedList 
     Map<Double, List<String>> sortedMap = new LinkedHashMap<>(); 
     for (Map.Entry<Double, List<String>> entry : list) 
     { 
      sortedMap.put(entry.getKey(), entry.getValue()); 
     } 

     return sortedMap; 
    } 
} 
+0

而且我只有一個數組列表,而不是每個@KinnarVasa不同的數組列表 – abc

相關問題