Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

What are the differences between Comparable and Comparator?

Pragya Keshap answered on February 4, 2023 Popularity 10/10 Helpfulness 3/10

Contents


More Related Answers

  • comparable class java
  • How to use java 8 comparator
  • java comparator comparing
  • How to use java 8 comparator
  • Comparable interface
  • Comparable interface
  • comparable

  • What are the differences between Comparable and Comparator?

    2

    Main differences between Comparable and Comparator are:

    1. Type: Comparable is an interface in Java where T is

    the type of objects that this object may be compared to.

    2. Comparator is also an interface where T is the type of

    objects that may be compared by this comparator.

    3. Sorting: In Comparable, we can only create one sort

    sequence. In Comparator we can create multiple sort

    sequences.

    4. Method Used: Comparator interface in Java has

    method public int compare (Object o1, Object o2) that

    returns a negative integer, zero, or a positive integer when

    the object o1 is less than, equal to, or greater than the

    object o2. A Comparable interface has method public

    int compareTo(Object o) that returns a negative integer,

    zero, or a positive integer when this object is less than,

    equal to, or greater than the object o.

    5. Objects for Comparison: The Comparator compares two

    objects given to it as input. Comparable interface

    compares "this" reference with the object given as input.

    6. Package location: Comparable interface in Java is defined

    in java.lang package. Comparator interface in Java is

    defined in java.util package

    https://www.javatpoint.com/difference-between-comparable-and-comparator#:~:text=1)%20Comparable%20provides%20a%20single,%2C%20name%2C%20and%20price%20etc.

    Popularity 10/10 Helpfulness 3/10 Language java
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on Feb 04 2023
    Pragya Keshap
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.