Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You get the wrong idea, actually.</p> <p>Here's the gist:</p> <ul> <li><code>Map&lt;K,V&gt;</code> is a mapping from <code>K key</code> to <code>V value</code></li> <li><code>TreeMap&lt;K,V&gt;</code> is a <code>SortedMap&lt;K,V&gt;</code> that sorts the <strong>keys</strong>, not the values</li> </ul> <p>So, a <code>TreeMap&lt;String,Person&gt;</code> would sort based on e-mail addresses, not the <code>Person</code>'s first/last names.</p> <p>If you need a <code>SortedSet&lt;Person&gt;</code>, or a sorted <code>List&lt;Person&gt;</code> then that's a different concept, and yes, <code>Person implements Comparable&lt;Person&gt;</code>, or a <code>Comparator&lt;Person&gt;</code> would come in handy.</p> <h3>API links</h3> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html" rel="nofollow noreferrer"><code>java.lang.Comparable&lt;T&gt;</code></a> - defines the "natural ordering" of objects of a type</li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Comparator.html" rel="nofollow noreferrer"><code>java.util.Comparator&lt;T&gt;</code></a> - defines a "custom" comparison of objects of a type</li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Map.html" rel="nofollow noreferrer"><code>java.util.Map&lt;K,V&gt;</code></a> - maps keys to values, not the other way around</li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/SortedMap.html" rel="nofollow noreferrer"><code>java.util.SortedMap&lt;K,V&gt;</code></a> - sorts the keys, not the values</li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/SortedSet.html" rel="nofollow noreferrer"><code>java.util.SortedSet&lt;E&gt;</code></a> - a set that is ordered</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>java.util.Collections.sort(List)</code></a> - a utility method to sort <ul> <li>Also has an overload that takes a <code>Comparator</code></li> </ul></li> </ul> <h3>Related questions</h3> <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/1206073/sorting-a-collection-of-objects">Sorting a collection of objects</a></li> <li><a href="https://stackoverflow.com/questions/1814095/sorting-an-arraylist-of-contacts">Sorting an ArrayList of Contacts</a></li> <li><a href="https://stackoverflow.com/questions/1440006/java-sortedmap-treemap-comparable-how-to-use">Java: SortedMap, TreeMap, Comparable? How to use?</a></li> </ul> <hr> <h3>Example</h3> <p>There are plenty of examples out there already, but here's one more:</p> <pre><code>import java.util.*; public class Example { static String lastName(String fullName) { return fullName.substring(fullName.indexOf(' ') + 1); } public static void main(String[] args) { Map&lt;String,String&gt; map = new TreeMap&lt;String,String&gt;(); map.put("001", "John Doe"); map.put("666", "Anti Christ"); map.put("007", "James Bond"); System.out.println(map); // "{001=John Doe, 007=James Bond, 666=Anti Christ}" // Entries are sorted by keys! // Now let's make a last name Comparator... Comparator&lt;String&gt; lastNameComparator = new Comparator&lt;String&gt;() { @Override public int compare(String fullName1, String fullName2) { return lastName(fullName1).compareTo(lastName(fullName2)); } }; // Now let's put all names in a SortedSet... SortedSet&lt;String&gt; namesByLastName = new TreeSet&lt;String&gt;(lastNameComparator); namesByLastName.addAll(map.values()); System.out.println(namesByLastName); // "[James Bond, Anti Christ, John Doe]" // Names sorted by last names! // Now let's use a List instead... List&lt;String&gt; namesList = new ArrayList&lt;String&gt;(); namesList.addAll(map.values()); System.out.println(namesList); // "[John Doe, James Bond, Anti Christ]" // These aren't sorted yet... Collections.sort(namesList); System.out.println(namesList); // "[Anti Christ, James Bond, John Doe]" // Sorted by natural ordering! // Now let's sort by string lengths... Collections.sort(namesList, new Comparator&lt;String&gt;() { @Override public int compare(String s1, String s2) { return Integer.valueOf(s1.length()).compareTo(s2.length()); } }); System.out.println(namesList); // "[John Doe, James Bond, Anti Christ]" // SUCCESS!!! } } </code></pre>
    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