如何根据其值对树图进行sorting?

我怎样才能使用它的值而不是键来sorting树图?

你不能像TreeMap的比较器仅仅在键上运行,例如看到这个构造函数 。

无论如何,你可以使用多个集合,使用TreeMap(或者说HashMap)通过键来查找元素,并且有一个SortedSet来迭代这些值。

这是一个解决scheme:

public static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) { Comparator<K> valueComparator = new Comparator<K>() { public int compare(K k1, K k2) { int compare = map.get(k2).compareTo(map.get(k1)); if (compare == 0) return 1; else return compare; } }; Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator); sortedByValues.putAll(map); return sortedByValues; } 

请注意,地图是从最高值到最低值sorting的。

Google Collections提供了一个TreeMultiMap 。

你也可以使用两个集合。 你想完成什么? 你能解释你的用例吗?

Apache Commons Collections有一个TreeBidiMap :

这个类保证地图将按照升序键顺序和升序值顺序排列,按照键和值的类的自然顺序sorting。

这里有一个Java5generics端口。

尝试下面的代码,它为我工作正常。 您可以select升序以及降序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()); } } } 

在创buildTreeMap时,您可以尝试给比较器比较值而不是键。

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

交换值和密钥。

更严重的是,请提供一些你想达到的背景。 也许在其他处理完成后sorting就足够了。