Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 109093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:00:54+00:00 2026-05-11T02:00:54+00:00

What is the difference between compare() and compareTo() methods in Java? Do those methods

  • 0

What is the difference between compare() and compareTo() methods in Java? Do those methods give the same answer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:00:55+00:00Added an answer on May 11, 2026 at 2:00 am

    From JavaNotes:

    • a.compareTo(b):
      Comparable interface : Compares values and returns an int which tells if the values compare less than, equal, or greater than.
      If your class objects have a natural order, implement the Comparable<T> interface and define this method. All Java classes that have a natural ordering implement Comparable<T> – Example: String, wrapper classes, BigInteger

    • compare(a, b):
      Comparator interface : Compares values of two objects. This is implemented as part of the Comparator<T> interface, and the typical use is to define one or more small utility classes that implement this, to pass to methods such as sort() or for use by sorting data structures such as TreeMap and TreeSet. You might want to create a Comparator object for the following:

      • Multiple comparisons. To provide several different ways to sort something. For example, you might want to sort a Person class by name, ID, age, height, … You would define a Comparator for each of these to pass to the sort() method.
      • System class To provide comparison methods for classes that you have no control over. For example, you could define a Comparator for Strings that compared them by length.
      • Strategy pattern To implement a Strategy pattern, which is a situation where you want to represent an algorithm as an object that you can pass as a parameter, save in a data structure, etc.

    If your class objects have one natural sorting order, you may not need compare().


    Summary from http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html

    Comparable
    A comparable object is capable of comparing itself with another object.

    Comparator
    A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances.


    Use case contexts:

    Comparable interface

    The equals method and == and != operators test for equality/inequality, but do not provide a way to test for relative values.
    Some classes (eg, String and other classes with a natural ordering) implement the Comparable<T> interface, which defines a compareTo() method.
    You will want to implement Comparable<T> in your class if you want to use it with Collections.sort() or Arrays.sort() methods.

    Defining a Comparator object

    You can create Comparators to sort any arbitrary way for any class.
    For example, the String class defines the CASE_INSENSITIVE_ORDER comparator.


    The difference between the two approaches can be linked to the notion of:
    Ordered Collection:

    When a Collection is ordered, it means you can iterate in the collection in a specific (not-random) order (a Hashtable is not ordered).

    A Collection with a natural order is not just ordered, but sorted. Defining a natural order can be difficult! (as in natural String order).


    Another difference, pointed out by HaveAGuess in the comments:

    • Comparable is in the implementation and not visible from the interface, so when you sort you don’t really know what is going to happen.
    • Comparator gives you reassurance that the ordering will be well defined.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 60k
  • Answers 60k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You can use this to get most of the HTML… May 11, 2026 at 9:33 am
  • added an answer I believe properties are either navigator.language or navigator.userLanguage. May 11, 2026 at 9:33 am
  • added an answer We use components (not free) from IDAutomation for PDF417. They… May 11, 2026 at 9:33 am

Related Questions

What is the difference between compare() and compareTo() methods in Java? Do those methods
What is the difference between early and late binding?
What is the difference between TrueType fonts and Type-1 fonts?
What is the difference between Build Solution and Batch Build in Visual Studio 2008?
What is the difference between a Group and a Match in .NET's RegEx?
What is the difference between publishing a website with visual studio and just copying
What is the difference between different optimization levels in GCC? Assuming I don't care
What is the difference between the AddRange and Concat functions on a generic List?
What is the difference between display:block and display:inline
What is the difference between the following class methods? Is it that one is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.