如何使用键和值对TreeMap进行排序

时间:2015-07-13 05:16:53

标签: android sorting arraylist treemap

是否可能是treemap ??,实际上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}

3 个答案:

答案 0 :(得分:0)

之后排序。

获取键值条目列表,并使用自定义Comparator对其进行排序。不幸的是,值是一个字符串列表,可能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;
    });

for(Map.Entry&gt; entry:entries){        进入    }

也许List<String>本身应该是SortedSet<String>TreeSet<String>

答案 1 :(得分: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;
    }

答案 2 :(得分: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;
    }
}