Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't have the <code>TreeMap</code> itself sort on the values, since that defies the <a href="http://java.sun.com/javase/6/docs/api/java/util/SortedMap.html" rel="noreferrer"><code>SortedMap</code></a> specification:</p> <blockquote> <p>A <code>Map</code> that further provides a <em>total ordering</em> on its <strong>keys</strong>. </p> </blockquote> <p>However, using an external collection, you can always sort <a href="http://java.sun.com/javase/6/docs/api/java/util/Map.html#entrySet%28%29" rel="noreferrer"><code>Map.entrySet()</code></a> however you wish, either by keys, values, or even a combination(!!) of the two.</p> <p>Here's a generic method that returns a <code>SortedSet</code> of <code>Map.Entry</code>, given a <code>Map</code> whose values are <code>Comparable</code>:</p> <pre><code>static &lt;K,V extends Comparable&lt;? super V&gt;&gt; SortedSet&lt;Map.Entry&lt;K,V&gt;&gt; entriesSortedByValues(Map&lt;K,V&gt; map) { SortedSet&lt;Map.Entry&lt;K,V&gt;&gt; sortedEntries = new TreeSet&lt;Map.Entry&lt;K,V&gt;&gt;( new Comparator&lt;Map.Entry&lt;K,V&gt;&gt;() { @Override public int compare(Map.Entry&lt;K,V&gt; e1, Map.Entry&lt;K,V&gt; e2) { int res = e1.getValue().compareTo(e2.getValue()); return res != 0 ? res : 1; } } ); sortedEntries.addAll(map.entrySet()); return sortedEntries; } </code></pre> <p>Now you can do the following:</p> <pre><code> Map&lt;String,Integer&gt; map = new TreeMap&lt;String,Integer&gt;(); map.put("A", 3); map.put("B", 2); map.put("C", 1); System.out.println(map); // prints "{A=3, B=2, C=1}" System.out.println(entriesSortedByValues(map)); // prints "[C=1, B=2, A=3]" </code></pre> <p>Note that funky stuff will happen if you try to modify either the <code>SortedSet</code> itself, or the <code>Map.Entry</code> within, because this is no longer a "view" of the original map like <code>entrySet()</code> is.</p> <p>Generally speaking, the need to sort a map's entries by its values is atypical.</p> <hr> <h3>Note on <code>==</code> for <code>Integer</code></h3> <p>Your original comparator compares <code>Integer</code> using <code>==</code>. This is almost always wrong, since <code>==</code> with <code>Integer</code> operands is a reference equality, not value equality.</p> <pre><code> System.out.println(new Integer(0) == new Integer(0)); // prints "false"!!! </code></pre> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/1514910/when-comparing-two-integers-in-java-does-auto-unboxing-occur">When comparing two Integers in Java does auto-unboxing occur?</a> (NO!!!)</li> <li><a href="https://stackoverflow.com/questions/2831945/is-it-guaranteed-that-new-integeri-i-in-java">Is it guaranteed that <code>new Integer(i) == i</code> in Java?</a> (YES!!!)</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload