wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why will TreeMap<K,V>(Comparator) not compile? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why will TreeMap<K,V>(Comparator) not compile?" Watch "Why will TreeMap<K,V>(Comparator) not compile?" New topic
Author

Why will TreeMap<K,V>(Comparator) not compile?

Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
The compiler won't let me do this:



It says:

mapc.java:13: cannot find symbol
symbol : constructor TreeMap(mapsortd)
location: class java.util.TreeMap<java.lang.Integer,mapc>
TreeMap<Integer,mapc> d = new TreeMap<Integer,mapc>(desc)
^


It seems TreeMap can use generics, or a Comparator, but not both. I don't quite see why the compiler would object to this. Can anyone explain this?


Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

TreeMap takes a Comparator that compares the keys -- not the values.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
Right, thank you!

TreeMap<Integer, mapx> m = new TreeMap<Integer,mapx>(new mapdesc());

class mapdesc implements Comparator<Integer>
 
wood burning stoves
 
subject: Why will TreeMap<K,V>(Comparator) not compile?