【问题标题】:How to do sorting of TreeMap with key as well value also如何使用键和值对 TreeMap 进行排序
【发布时间】:2015-10-01 06:30:34
【问题描述】:

树图可以吗??实际上树图本身对键进行排序,但我也想对键和值进行排序。

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}

【问题讨论】:

  • 是的,您可以对其进行排序....我*会尽快发布答案。
  • TreeMap sort by value 的可能重复项
  • 没有从这个给定的链接得到解决方案,因为我使用 List 作为值。那么如何使用比较器获取值呢? @user3360241
  • 我认为您必须遵循键和值的计数排序原则。即键是第一个数字,第二个数字是值,然后可以将其排序为字符串数组。 sanfoundry.com/java-program-implement-counting-sort
  • 什么时候? @CodeProcessor

标签: android sorting arraylist treemap


【解决方案1】:

你可以使用这个方法:

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;
    }

【讨论】:

  • 谢谢,但我已经使用它不适用于我的情况@KinnarVasa
  • 您好像没有尝试运行上面的代码,请尝试一下并检查输出。
  • @abc,真奇怪!!
  • 我只是运行代码并得到输出:{0.21=[Swandwitch, 756334], 1.84=[Bruger, 234234234], 2.35=[Pizza, 342342], 5.89=[Rolls, 453453]}
  • 怎么样?当我尝试这段代码时,我得到了 0.21={Burger, 02058795247}, 1.84={Pizza, 02087958742}, 2.35={Rolls, 020547896874}, 5.89={Sandwich, 02058967412}。我的意思是当我将数据插入 arraylist 时,我得到相同的插入顺序没有得到理想的输出@KinnarVasa
【解决方案2】:

事后排序。

获取键值条目列表,并使用自定义比较器对其进行排序。不幸的是,该值是一个字符串列表,也许SortedSet&lt;String&gt; 可能更容易。

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> 条目:条目) { ... 入口 }

也许List&lt;String&gt; 本身应该是一个SortedSet&lt;String&gt;,一个TreeSet&lt;String&gt;

【讨论】:

    【解决方案3】:

    这是完整的代码:

    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;
        }
    }
    

    【讨论】:

    • 而且我只有一个数组列表,每个 @KinnarVasa 的数组列表没有不同
    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多