aspose file tools
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 Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 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: 19302
    
  41

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>
 
rocket mass heater kickstarter
 
subject: Why will TreeMap<K,V>(Comparator) not compile?