This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
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
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 16817
    
  19

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