Sorting the Map<Key,Value> in descending order based on the value 
Possible Duplicate:
  How to sort a Map on the values in Java?
我使用map接口读取文件,然后将其中的值作为键值对存储。文件格式如下
我将从这个文件中读取数据并将其存储为键值对,然后将其显示给用户。我的要求是以这种格式显示结果
因此,我需要按值的降序对映射进行排序。这样我就可以把这些作为我的结果显示出来。我已经阅读了相关内容并找到了以下代码
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | 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;
    } | 
我希望这将按升序对值进行排序,我只想知道这种方法是正确的还是其他有效的方法对我有帮助?
		
		
- 也许你应该看看番石榴。请参阅此评论。
- @Nandakumar:Set不允许重复,例如使用List。如果你开始允许的话,你就违反了Comparator的合同!
- ` map.entryset().stream().排序(comparator.comparising(e->-e.ge&zwnj;&8203;tValue()).foreach(s&zwnj;&8203;system.out::println);&zwnj;&8203;。`
 
	 
因为可以有重复的值,所以根本不应该使用Set。改为List,然后进行排序。你的entriesSortedByValues看起来像这样:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | 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;
} | 
注意:在您的示例中,输出值是降序的。如果你想让它们上升,用e1.getValue().compareTo(e2.getValue())代替。
例子:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | public static void main(String  args[]) { 
    Map<String , Integer>  map = new  HashMap<String , Integer>(); 
    map.put("A" , 34); 
    map.put("B" , 25); 
    map.put("C" , 50); 
    map.put("D" , 50); //"duplicate" value
    System .out .println( entriesSortedByValues( map));
} | 
输出:
| 1
 | [D=50, C=50, A=34, B=25] | 
		
		
- 为我工作。谢谢!
- 每个人都应该使用sortedEntries.sort(),而不是Collections:sortedEntries.sort((e1, e2) -> e2.getValue().compareTo(e1.getValue()));。
 
	 
写下你自己的comparator,交给TreeMap。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | 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));
}
} | 
在测试课上
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | 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); | 
输出:
		
		
- 如果添加lMap.put("D", 50),这不起作用,因为它将被视为一个副本(事实上,它会覆盖50的任何其他值,例如"C")。
- 不允许重复值。
- @谢谢你的友好解决方案。但是,我希望允许重复的值。有什么可能达到同样的效果呢?谢谢您。
- 以下代码将起作用。public map sortByValue(map map)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(iterator it=list.iterator();it.hasNext();)map.entry entry=(map.entry)it.next();result.put(entry.getkey(),entry.getValue());返回结果;
- 您应该使用new treemap(collections.reverseOrder());。mapnew map=new treemap(collections.reverseOrder());new map.putall(mymap);