Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: Object to C...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 3 Topic 15974 of 16269
Post > Topic >>

Re: Object to Comparable

by Eric Sosman <Eric.Sosman@[EMAIL PROTECTED] > Apr 15, 2008 at 05:18 PM

Andrew Marcus wrote:
> I am implemented a program LinkList based Binarytree but I want to use
> Comparable for comparing the values stored at two different Nodes and
> the values I stored at each Node are of type Objects.Can anyone please
> help?I shall be very grateful if anyone provide me the least of
> suggestions also.

     Do you mean java.lang.Comparable or java.util.Comparator?
The two interfaces are rather different:

     - A class that implements Comparable must provide a
       compareTo method that takes one argument, a reference
       to an object of that same class.  It compares "this"
       the the argument object.

     - A class that implements Comparator must provide a
       compare method that takes *two* arguments, referring
       to the two objects to be compared.  They are almost
       certainly not of the same class as the Comparator;
       the whole point of a Comparator is to determine an
       order for a class that doesn't implement Comparable,
       or whose Comparable defines a different order than
       the one you want.

     Comparable is a schoolchild boasting "I'm taller than
you are."  Comparator is the teacher who says "Johnny got
a better quiz score than Frankie did."

     Finally, are you really using undifferentiated Objects
as the nodes of your tree?  That seems pointless; a plain
Object doesn't implement Comparable and doesn't have any
useful attributes a Comparator could use.  More likely (I
guess) you're using Object as a place-holder for a node of
some more specialized type.  If so, I'll suggest two ways
to proceed:

     - Insist that the nodes be of a class that implements
       Comparable.  The easiest way to do this is to write
       the tree's methods to take Comparable references
       rather than unspecialized Object references.

     - Use Objects, but require that the user provide a
       Comparator that defines an ordering on the nodes --
       a convenient way to do this might be to take the
       Comparator as an argument to the tree's constructor.

     You can even combine the two approaches; see, for example,
java.util.SortedSet.

     Finally squared, the business of using plain Object or
plain Comparable or plain Comparator is now passé.  For the
past few years, the cognoscenti have been using something
called "generics," which require more typing than one might
like but are convenient nonetheless.  The on-line Java Tutorial
has a couple sections on generics, one easy and one a little
deeper; you'd do well to take a look at them.

-- 
Eric.Sosman@[EMAIL PROTECTED]

 




 3 Posts in Topic:
Object to Comparable
Andrew Marcus <mainhoo  2008-04-15 09:11:40 
Re: Object to Comparable
Eric Sosman <Eric.Sosm  2008-04-15 17:18:19 
Re: Object to Comparable
Roedy Green <see_websi  2008-04-20 10:40:58 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 4:56:33 CDT 2008.