File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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

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: 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

Joined: Sep 28, 2004
Posts: 18108

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


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>
I agree. Here's the link:
subject: Why will TreeMap<K,V>(Comparator) not compile?
Similar Threads
TreeMap uses compare/compareTo
K&B chapter 7 Q7 question HashMap vs TreeMap
generic comparator problem
HashMap sorting
Discussing errata for K&B, SCJP 6