How to sort a treemap based on its values?

前端 未结 9 1010
萌比男神i
萌比男神i 2020-12-01 04:53

How can I sort a treemap using its values rather than the key?

相关标签:
9条回答
  • 2020-12-01 05:16

    You could try giving a Comparator that compare values instead of keys when you create the TreeMap.

        final TreeMap<Integer,String> tree = new TreeMap<Integer,String>();
        tree.put(1, "1");
        tree.put(2, "2");
        tree.put(3, "3");
        tree.put(4, "4");
    
        final TreeMap<Integer,String> treeSortedByValues = new TreeMap<Integer,String>(new Comparator<Integer>()
        {
            public int compare(Integer o1, Integer o2)
            {
                return tree.get(o1).compareTo(tree.get(o2));
            }
        });
        treeSortedByValues.putAll(tree);
    
        for ( Entry<Integer, String> e : treeSortedByValues.entrySet() )
        {
            System.out.println(e.getKey() + ": " + e.getValue());
        }
    
    0 讨论(0)
  • 2020-12-01 05:17

    Try this. This sorts TreeMap values in ascending order, assuming that is how you want the values to be sorted.

    static <K, V> Map<K, V> sortByValues(Map<K, V> map) {
            List<?> list = new ArrayList(map.entrySet());
    
            // copy Map to List to use Comparator
            Collections.sort(list, new Comparator() {
                public int compare(Object o1, Object o2) {
                    return ((Comparable) ((Map.Entry) o1).getValue()).compareTo(((Map.Entry) o2).getValue());
                }
            });
    
            // then copy List to LinkedHashMap as it preserves insertion order
            Map<K, V> result = new LinkedHashMap<K, V>();
            Iterator itr = list.iterator();
            while (itr.hasNext()) {
                Map.Entry<K, V> m = (Map.Entry<K, V>) itr.next();
                result.put(m.getKey(), m.getValue());
            }
    
            return result;
        }
    
    0 讨论(0)
  • 2020-12-01 05:18

    Apache Commons Collections has a TreeBidiMap:

    This class guarantees that the map will be in both ascending key order and ascending value order, sorted according to the natural order for the key's and value's classes.

    There's a Java5-generics port of it here.

    0 讨论(0)
  • 2020-12-01 05:22

    Google Guava provides a TreeMultiMap.

    You could also use two collections. What are you trying to accomplish? Can you explain your use cases?

    0 讨论(0)
  • 2020-12-01 05:23

    You cannot as the TreeMap's comparator is run against the keys only, e.g. see this constructor.

    Anyway, you can use multiple Collections, use the TreeMap (or rather HashMap) for looking up elements by keys, and have a SortedSet to iterate on the values.

    0 讨论(0)
  • 2020-12-01 05:26

    Try below code it works fine for me. You can choose both ascending as well as descending order for sorting.

    package com.rais;
    
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class SortMapByValue
    {
        public static boolean ASC = true;
        public static boolean DESC = false;
    
        public static void main(String[] args)
        {
    
            // Creating dummy unsorted map
            Map<String, Integer> unsortMap = new HashMap<String, Integer>();
            unsortMap.put("B", 55);
            unsortMap.put("A", 80);
            unsortMap.put("D", 20);
            unsortMap.put("C", 70);
    
            System.out.println("Before sorting......");
            printMap(unsortMap);
    
            System.out.println("After sorting ascending order......");
            Map<String, Integer> sortedMapAsc = sortByComparator(unsortMap, ASC);
            printMap(sortedMapAsc);
    
    
            System.out.println("After sorting descindeng order......");
            Map<String, Integer> sortedMapDesc = sortByComparator(unsortMap, DESC);
            printMap(sortedMapDesc);
    
        }
    
        private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap, final boolean order)
        {
    
            List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet());
    
            // Sorting the list based on values
            Collections.sort(list, new Comparator<Entry<String, Integer>>()
            {
                public int compare(Entry<String, Integer> o1,
                        Entry<String, Integer> o2)
                {
                    if (order)
                    {
                        return o1.getValue().compareTo(o2.getValue());
                    }
                    else
                    {
                        return o2.getValue().compareTo(o1.getValue());
    
                    }
                }
            });
    
            // Maintaining insertion order with the help of LinkedList
            Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
            for (Entry<String, Integer> entry : list)
            {
                sortedMap.put(entry.getKey(), entry.getValue());
            }
    
            return sortedMap;
        }
    
        public static void printMap(Map<String, Integer> map)
        {
            for (Entry<String, Integer> entry : map.entrySet())
            {
                System.out.println("Key : " + entry.getKey() + " Value : "+ entry.getValue());
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题