Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default Comparable vs. Comparator interface?

    Hi,

    I'm still having trouble understanding the difference with these two interface types.

    When do we use comparable, and when do we use comparator? What can comparator do which comparable can't?

    I understand that compareTo() compares a implicit parameter with a explicit parameter, where compare() compares two explicit parameters, but what does this difference mean?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Comparable vs. Comparator interface?

    Comparable is an interface generally implemented by a class that is Comparable.
    In other words, a class which you want to assign a "natural order" to.
    You use a Comparator to provide ordering for something that does not have a natural order, or whose order for a particular List (for example) is not the same as its natural order.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Comparable interface
    By diamonddragon in forum New To Java
    Replies: 17
    Last Post: 02-01-2012, 12:07 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. Comparable Interface
    By Yelrubk in forum New To Java
    Replies: 3
    Last Post: 04-28-2010, 02:46 PM
  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
  •