title: Java: SortedMap, TreeMap, Comparable? How to use?
body: <p>I have a list of objects I need to sort according to properties of one of their fields. I've heard that SortedMap and Comparators are the best way to do this.</p>
  
  <ol>
  <li>Do I implement Comparable with the class I'm sorting, or do I create a new class?</li>
  <li>How do I instantiate the SortedMap and pass in the Comparator?</li>
  <li>How does the sorting work? Will it automatically sort everything as new objects are inserted?</li>
  </ol>
  
  <p><strong>EDIT:</strong>
  This code is giving me an error:</p>
  
  <pre><code>private TreeMap&lt;Ktr&gt; collection = new TreeMap&lt;Ktr&gt;();
  </code></pre>
  
  <p>(Ktr implements <code>Comparator&lt;Ktr&gt;</code>). Eclipse says it is expecting something like <code>TreeMap&lt;K, V&gt;</code>, so the number of parameters I'm supplying is incorrect.</p>