Possible Duplicate:
How can I sort the keys of a Map in Java?
In class TreeMap the Java API says:
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.
What is meant by natural ordering ? A class used as key does not have to implement the Comparable interface, but what ordering will be used instead ?
If you were to try this yourself you’d find that you cannot use a
TreeMapthat has aKthat does not implementComparable(Unless you explicitly provide aComparatorvia theTreeMapconstructor).The javadoc for
put()states this explicitly:The link in javadocs for TreeMap for “Natural Ordering” takes you to the
Comparableinterface