Java 对地图进行排序<;关键、价值>;根据值按降序排列

Java 对地图进行排序<;关键、价值>;根据值按降序排列,java,map,hashmap,Java,Map,Hashmap,可能重复: 我使用map接口读取文件,然后将其中的值作为键值对存储。文件格式如下 A 34 B 25 c 50 我将从该文件中读取数据,并将其存储为键值对,然后将其显示给用户。我的要求是以这种格式显示结果 C 50 A 34 B 25 因此,我需要按照值的降序对映射进行排序。这样我就可以显示这些结果。。我已经读过了,找到了下面的代码 static <K,V extends Comparable<? super V>> SortedSet<Map.Entr

可能重复:

我使用map接口读取文件,然后将其中的值作为键值对存储。文件格式如下

 A 34
 B 25
 c 50
我将从该文件中读取数据,并将其存储为键值对,然后将其显示给用户。我的要求是以这种格式显示结果

C 50
A 34
B 25
因此,我需要按照值的降序对映射进行排序。这样我就可以显示这些结果。。我已经读过了,找到了下面的代码

static <K,V extends Comparable<? super V>> SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) {
        SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>(
            new Comparator<Map.Entry<K,V>>() {
                @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) {
                    int res = e1.getValue().compareTo(e2.getValue());
                    return res != 0 ? res : 1; // Special fix to preserve items with equal values
                }
            }
        );
        sortedEntries.addAll(map.entrySet());
        return sortedEntries;
    }

static因为可以有重复的值,所以根本不应该使用
集。更改为
列表
,并对其进行排序。您的
entriessortedbyvalue
将如下所示:

static <K,V extends Comparable<? super V>> 
            List<Entry<K, V>> entriesSortedByValues(Map<K,V> map) {

    List<Entry<K,V>> sortedEntries = new ArrayList<Entry<K,V>>(map.entrySet());

    Collections.sort(sortedEntries, 
            new Comparator<Entry<K,V>>() {
                @Override
                public int compare(Entry<K,V> e1, Entry<K,V> e2) {
                    return e2.getValue().compareTo(e1.getValue());
                }
            }
    );

    return sortedEntries;
}
输出:

[D=50, C=50, A=34, B=25]

编写自己的
比较器
并将其传递到
树形图

class MyComparator implements Comparator {

Map map;

public MyComparator(Map map) {
    this.map = map;
}

public int compare(Object o1, Object o2) {

    return ((Integer) map.get(o2)).compareTo((Integer) map.get(o1));

}
}
在考试班

Map<String, Integer> lMap=new HashMap<String, Integer>();
    lMap.put("A", 35);
    lMap.put("B", 25);
    lMap.put("C", 50);

    MyComparator comp=new MyComparator(lMap);

    Map<String,Integer> newMap = new TreeMap(comp);
    newMap.putAll(lMap);

也许你应该看看番石榴。请参阅注释。@NandaKumar:
Set
s不允许重复,例如使用
List
。如果你开始允许他们,你就违反了比较器的契约map.entrySet().stream().sorted(Comparator.comparing(e->-e.getValue()).forEach(System.out::println)`如果您添加
lMap.put(“D”,50)
,这将不起作用,因为它将被视为一个重复值(并且实际上将用
50
覆盖任何其他值,例如
“C”
)。不允许重复值。@Amingh:谢谢您的友好解决方案。但是,我希望允许重复值。有什么可能达到同样的效果呢。谢谢。下面的代码将起作用。公共映射sortByValue(Map Map){List List=new LinkedList(Map.entrySet());Collections.sort(List,new Comparator(){public int compare(Object o2,Object o1){return((Comparable)((Map.Entry)(o1)).getValue()).compareTo((Map.Entry)(o2)).getValue();});Map result=new LinkedHashMap();for(迭代器it=list.Iterator();it.hasNext();){Map.Entry条目=(Map.Entry)it.next();result.put(Entry.getKey(),Entry.getValue());}返回结果;}应该使用新树映射(Collections.reverseOrder());。Map newMap=newtreemap(Collections.reverseOrder());newMap.putAll(myMap);对我有用。谢谢每个人都应该使用
sortedEntries.sort()
而不是像这样的
Collections
sortedEntries.sort((e1,e2)->e2.getValue().compareTo(e1.getValue())
Map<String, Integer> lMap=new HashMap<String, Integer>();
    lMap.put("A", 35);
    lMap.put("B", 25);
    lMap.put("C", 50);

    MyComparator comp=new MyComparator(lMap);

    Map<String,Integer> newMap = new TreeMap(comp);
    newMap.putAll(lMap);
C=50
A=35
B=25