wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TreeMap ,TreeSet with Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TreeMap ,TreeSet with Comparator" Watch "TreeMap ,TreeSet with Comparator" New topic
Author

TreeMap ,TreeSet with Comparator

Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
I am trying to run a following Prgm on TreeMap and TreeSet with Comparator.



The compiler is flaging an error at <<<<<<< Not compling.However removing the argument com makes it compilable.

Other Inforamtion:
  • AgeComparator is a Comparator which compares Dogs based on their names.
  • The constructor of the Dog has first argument age and second argument name.
  • Dog implements Comparable.


  • What mistake I might be doing?Ranchers ,Please help.
    [ December 18, 2006: Message edited by: Sanjeev Kumar Singh ]

    ~Sanjeev Singh<br />SCJP 1.5
    Mark Uppeteer
    Ranch Hand

    Joined: Mar 02, 2004
    Posts: 159

    Hi Sanjeev Kumar Singh
    Your comparator compares Dogs, but it should compare Strings because String is the type of your key, and it is according to the key that the sorty will happen.

    regards,
    Mark


    I know where my towel is. (SCJP 5, OCPJWCD)
    [Free Quiz Tips for a fun night with friends or family] Flash games
    Sanjeev Singh
    Ranch Hand

    Joined: Nov 01, 2006
    Posts: 381
    Thanks very much Mark,
    I was under the wrong impression that it is the value whose type is responsible for the sorting.
    I have made the changes and now it is running..

     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: TreeMap ,TreeSet with Comparator
     
    Similar Threads
    Comparator and Compatable Query
    Collections Comparator....in how much detail??
    Comparator ClassCastException
    generics...
    TreeSet add method