2015-07-13 111 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

相关问题