If you try this on your own, you will find that you cannot use a TreeMap that has a K that does not implement Comparable (unless you explicitly provide a Comparator via the TreeMap constructor).
public class App { public static void main( String[] args ) { TreeMap<App,String> tm = new TreeMap<App,String>(); tm.put(new App(), "value"); } }
Exception in thread "main" java.lang.ClassCastException: application cannot be added to java.lang.Comparable
The javadoc for put() states this explicitly:
Throws:
ClassCastException - if the specified key cannot compare with the keys currently on the map
The link in javadocs for TreeMap for "Natural Ordering" will lead you to the Comparable interface
source share