Sorting Descending order: Java Map

前端 未结 5 1879
不思量自难忘°
不思量自难忘° 2020-12-05 04:28

What I want to do is sort a map by value. I went over many questions that are available on the stackoverflow site and found out following solution that does what I want but

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 04:44

        TreeMap treeMap = new TreeMap();
    
        NavigableMap  nmap = treeMap.descendingMap();
    
        Set set = nmap.entrySet();
    
        Iterator iterator = set.iterator();
    

    now u can iterate over iterator and extract the value using iterator.hasNext() and iterator.next() methods ......

提交回复
热议问题