Results 1 to 10 of 10
  1. #1
    lenjaku is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Can I mix comparable and comparator?

    As long as I am concerned I can't since both are interfaces but yet I am asked to do so....(or so it seems)
    So I am confused.

    I am asked to create a code that if a user enters 1 it will use the object natural comparison form ('default') as written in CompareTo method.
    But if he chooses to enter something else then another comparison is used.

    Any idea how it's done?
    Maybe I just need to use 2 diff comparators? but then what;s the point of defining something as 'default'....

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,907
    Rep Power
    5

    Default Re: Can I mix comparable and comparator?

    Why do you think it would be a problem to "mix" both? They're simply two different solutions to the same problem.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    lenjaku is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Can I mix comparable and comparator?

    Because both are interfaces how can I implement both?

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,907
    Rep Power
    5

    Default Re: Can I mix comparable and comparator?

    Well you can implement multiple interfaces on the same object, but in this particular case I wouldn't recommend that. Whenever I create a comparator, I tend to make it a static inner class. That way it is separated from the class you want to compare and yet still tightly coupled to it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    lenjaku is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Can I mix comparable and comparator?

    I will read into it then, we are not allowed to create classes using the word class....so I guess I;d make it Anonymous.
    Could you please give me the base structure of it?
    (If it's not rude of me to ask).

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,538
    Rep Power
    5

    Default Re: Can I mix comparable and comparator?

    Quote Originally Posted by lenjaku View Post
    I will read into it then, we are not allowed to create classes using the word class....so I guess I;d make it Anonymous.
    Hmm, good luck getting your program to run. I hope this is the instructor's way of saying "for this exercise, please use only anonymous classes in the creation of your interfaces." In any event, check out the syntax here --> Anonymous Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Regards,
    Jim
    Last edited by jim829; 01-24-2014 at 04:59 PM. Reason: typo
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,907
    Rep Power
    5

    Default Re: Can I mix comparable and comparator?

    Quote Originally Posted by lenjaku View Post
    I will read into it then, we are not allowed to create classes using the word class....so I guess I;d make it Anonymous.
    Could you please give me the base structure of it?
    (If it's not rude of me to ask).
    Its not rude, its just wrong of you to ask. You need to do this, not me. The difficult learning path towards getting this done is the thing you need and will make you understand, not me giving the answer.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    lenjaku is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Can I mix comparable and comparator?

    Thank you and sorry for the late response.
    I already created an Anonymous class for an iterator yesterday, though I lack confidence in my work since we didn't really learn it...just were sent to research it (we were not told this is what we are supposed to use), I wanted to have a base example...I feel like I am guessing 'till things are working...

    I am still not sure how I combine compareTo and Comparator though.

    If I have a class that implements comparable, and then I create an instance of it and I add an inner comparator, which 1 will be used?
    If I choose 2 different types for each comparison I could guess the compiler chooses the one that fits but what if both work on int, for example?

    In my case they work on different types but I'd still want to know

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,538
    Rep Power
    5

    Default Re: Can I mix comparable and comparator?

    When you implement this you are simply providing the methods to do the comparison. The choice of which method to use is dictated by the class that is doing the comparing. So if one class expects a type of Comparable, it will use the compareTo method. If it expects a type of Comparator, it will use the compare method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    lenjaku is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Comparable vs. Comparator interface?
    By Armin Beganovic in forum New To Java
    Replies: 1
    Last Post: 07-30-2012, 11:41 AM
  2. Replies: 0
    Last Post: 10-04-2011, 10:45 PM
  3. Comparable and Comparator
    By jeanjiang in forum New To Java
    Replies: 7
    Last Post: 04-23-2011, 07:59 AM
  4. how to use comparator
    By KidneyinaCooler in forum Advanced Java
    Replies: 2
    Last Post: 07-18-2010, 10:25 AM
  5. Using Comparable and Comparator interfaces
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •