Sort map in descending order java8
<pre><code>private static &lt;K, V extends Comparable&lt;? super V&gt;&gt; Map&lt;K, V&gt;
    sortByValue( Map&lt;K, V&gt; map )
    {
        Map&lt;K, V&gt; result = new LinkedHashMap&lt;&gt;();
        Stream&lt;Map.Entry&lt;K, V&gt;&gt; st = map.entrySet().stream();

        st.sorted( Map.Entry.comparingByValue() )
                .forEachOrdered( e -&gt; result.put(e.getKey(), e.getValue()) );

        return result;
    }
</code></pre>

<p>This is an example from <a href="https://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values-java?page=1&amp;tab=votes#tab-top">this</a> post. It works. the problem is that it sorts in ascending order. How can I change it to descending ?</p>

<p>I can do that like this:</p>

<pre><code>public static &lt;K, V extends Comparable&lt;? super V&gt;&gt; Map&lt;K, V&gt;
sortByValue( Map&lt;K, V&gt; map )
{
    List&lt;Map.Entry&lt;K, V&gt;&gt; list =
            new LinkedList&lt;Map.Entry&lt;K, V&gt;&gt;( map.entrySet() );
    Collections.sort( list, new Comparator&lt;Map.Entry&lt;K, V&gt;&gt;()
    {
        public int compare( Map.Entry&lt;K, V&gt; o1, Map.Entry&lt;K, V&gt; o2 )
        {
            return (o2.getValue()).compareTo( o1.getValue() );//change o1 with o2
        }
    } );

    Map&lt;K, V&gt; result = new LinkedHashMap&lt;K, V&gt;();
    for (Map.Entry&lt;K, V&gt; entry : list)
    {
        result.put( entry.getKey(), entry.getValue() );
    }
    return result;
}
</code></pre>

<p>I can do that by changing order i this line: <code>return (o2.getValue()).compareTo( o1.getValue() );</code> But I would like to try with lambda expr.</p>