Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>TreeMap</code> is a <a href="http://java.sun.com/javase/6/docs/api/java/util/SortedMap.html" rel="nofollow noreferrer"><code>SortedMap</code></a>, which sorts by keys. The way you were using it, you have <code>String</code> keys, and its natural ordering is lexicographical, where <code>"11" &lt; "2"</code>.</p> <p>Here's an illustration using <code>List</code> to simplify:</p> <pre><code>List&lt;String&gt; list = new ArrayList&lt;String&gt;( Arrays.asList("2", "1", "11", "b", "a", "aa") ); Collections.sort(list); System.out.println(list); // prints "[1, 11, 2, a, aa, b]" </code></pre> <p>Note the analogy between <code>1, 11, 2</code> and <code>a, aa, b</code>. This is precisely what lexicographical ordering mandates. In a dictionary, you expect <code>aa</code> to appear before <code>b</code>. Similary, in this ordering, you expect <code>11</code> to appear before <code>2</code>.</p> <h3>See also</h3> <ul> <li><a href="http://en.wikipedia.org/wiki/Lexicographical_order" rel="nofollow noreferrer">Wikipedia/Lexicographical order</a></li> </ul> <hr> <p>If you need to compare them as <code>Integer</code>, use a custom <code>Comparator&lt;String&gt;</code>:</p> <pre><code>Comparator&lt;String&gt; stringAsInteger = new Comparator&lt;String&gt;() { @Override public int compare(String s1, String s2) { return Integer.valueOf(s1).compareTo(Integer.valueOf(s2)); } }; List&lt;String&gt; list = new ArrayList&lt;String&gt;( Arrays.asList("2", "1", "10", "007") ); Collections.sort(list, stringAsInteger); System.out.println(list); // prints "[1, 2, 007, 10]" SortedMap&lt;String, String&gt; map = new TreeMap&lt;String, String&gt;(stringAsInteger); map.put("2", "Two"); map.put("1", "One"); map.put("10", "Ten"); map.put("12", "Twelve"); map.put("7", "Seven"); System.out.println(map); // prints "{1=One, 2=Two, 7=Seven, 10=Ten, 12=Twelve}" </code></pre> <h3>API links</h3> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Comparator.html" rel="nofollow noreferrer"><code>java.util.Comparator</code></a></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29" rel="nofollow noreferrer"><code>Collections.sort</code></a> -- there are 2 overloads</li> </ul> <h3>Related questions</h3> <p>On <code>Comparator</code> and <code>Comparable</code></p> <ul> <li><a href="https://stackoverflow.com/questions/2266827/when-to-use-comparable-vs-comparator">When to use Comparable vs Comparator</a></li> <li><a href="https://stackoverflow.com/questions/420223/difference-between-compare-and-compareto">difference between compare() and compareTo()</a></li> <li><a href="https://stackoverflow.com/questions/2858628/comparable-and-comparator-contract-with-regards-to-null">Comparable and Comparator contract with regards to null</a></li> <li><a href="https://stackoverflow.com/questions/2892947/why-does-the-java-collections-framework-offer-two-different-ways-to-sort/">Why does the Java Collections Framework offer two different ways to sort?</a></li> </ul> <p>On sorting <code>Map</code> by values</p> <ul> <li><a href="https://stackoverflow.com/questions/2864840/treemap-sort-by-value">TreeMap sort by value</a></li> <li><a href="https://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java">How to sort a Map on the values in Java?</a></li> </ul> <hr> <h3>On raw types</h3> <p><em>Effective Java 2nd Edition, Item 23: Don't use raw types in new code</em></p> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it">What is a raw type and why shouldn’t we use it?</a></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. 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