Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you are confusing two different things. A <code>TreeMap</code> is implemented as a <b>Red-Black Tree</b>. <br/>Per the Java doc: <br/></p> <blockquote> <p>A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.</p> <p>This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.</p> </blockquote> <p>So, in essence if you want to have a predictable ordering of your keys you should either leave the TreeMap to order your keys by using natural ordering or implement the <code>Comparator</code> interface yourself. Again, from the Javadoc:<br/></p> <blockquote> <p>Note that the ordering maintained by a tree map, like any sorted map, and whether or not an explicit comparator is provided, must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a sorted map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.</p> </blockquote> <p>Now, it isn't clear if you want to place your keys in the way that I mentioned (i.e natural ordering) or in another way (i.e. insertion order or something else?).<br/> For example, if you prefer insertion order the <a href="http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html" rel="nofollow">LinkedHashMap</a> might prove better for your case. <br/>If something else is the case please specify it :].<br/></p>
    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. This table or related slice is empty.
    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