Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Difference between Comparable and Comparator

  • Ohms law Definition, Formula, Statement
    Ohms law Definition, Formula, Statement | ओम का नियम Science
  • Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी
    Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी Biography
  • Newton's laws of Motion, State and Explained, Formula
    Newton’s laws of Motion, State and Explained, Formula Science
  • Amitabh Bachchan biography in hindi | Family, wife, awards
    Amitabh Bachchan biography in hindi | Family, wife, awards Biography
  • Ayodhya Kand in Hindi | अयोध्या काण्ड | राम को 14 वर्ष का वनवास
    Ayodhya Kand in Hindi | अयोध्या काण्ड | राम को 14 वर्ष का वनवास Spiritual
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography
  • Aranya Kand with Hindi Meaning | अरण्यकाण्ड | सीता हरण
    Aranya Kand with Hindi Meaning | अरण्यकाण्ड का अर्थ | सीता हरण Spiritual
  • Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म
    Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म Spiritual

Difference between Comparable and Comparator

Posted on August 16, 2021June 5, 2022 By GeekCer Education No Comments on Difference between Comparable and Comparator
Difference between Comparable and Comparator

You can use both the Comparable and Comparator for the sorting of the collection in Java. The Comparable itself compares two elements, but in case of comparator you need to create a sub-class and implement this interface. For this you need to write your own sorting code. Furthermore, we will discuss the difference between Comparator and Comparator in detail and in which package this interface is present and what methods are available in those interfaces.

Table of Contents

    • Comparable Interface in Java
      • compare(Object obj) method
    • Comparator Interface in Java
  • Comparable Vs Comparator

Comparable Interface in Java

Comparable defines a way to sort elements based on only one field, if we are referencing a user-defined class as an element. A user-defined class can have multiple fields but we can use only one field in comparison.

compare(Object obj) method

On the basis of comparison like greater than, less than and equal this method returns some number.

  • Returns +ve number, if the current object is greater than the provided object.
  • Returns -ve number, if the current object is less than the provided object.
  • If the current object is equal to the specified object then method will return 0.

Comparator Interface in Java

If you have to write your own sorting code then you should use Comparator interface which will help you to customize your sorting order. Hence, you can sort the collections based on multiple fields if you are going to sort collection which contains user-defined class.

Comparable Vs Comparator

Here is the main difference between Comparable and Comparator are as follow:

#ComparableComparator
1Comparable interface is used to sort the objects with natural ordering.But Comparator is meant for customize sorting order.
2Comparable interface compares current object (using “this”) reference with the specified object.But Comparator in Java compares two different class objects.
3Comparable is present in java.lang package.But Comparator is present in the java.util package.
4Comparable has compareTo() method to sort elements.But Comparator has compare() method to sort elements.
5Comparable affects the original class, i.e., which is modified.But Comparator does not affect the original class.
6Collections.sort(List) method can be used to sort the list elements of Comparable type.But Collections.sort(List, Comparator) method can be used to sort the list elements of Comparator type.

Recommended Article

  • Difference Between Callable Interface and Runnable Interface
  • Difference between structured data and unstructured data
  • final, finally and finalize difference
  • StringBuilder Vs StringBuffer in Java
  • Difference between Abstract Class and Interface
  • Memory Allocation – Stack Memory Vs Heap Memory

In conclusion, hope you have learned about difference between comparable and comparator. Feel free to contact us on this link.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Differences, Java

Post navigation

Previous Post: Difference between SQL and HQL | Hql vs Sql performance
Next Post: Difference between Jenkins and TeamCity, Maven, Octopus

More Related Articles

Difference between Jenkins and TeamCity Difference between Jenkins and TeamCity, Maven, Octopus Differences
Collections in Java Collections in Java Java
Difference between Structure and Class in C++ in tabular form Difference between Structure and Class in C++ in tabular form Differences
IPv4 Vs IPv6 | Difference between IPv4 and IPv6 IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
Top Java Programs for Coder Top Java Programs for Coder Java
Difference between SOAP and REST Difference between SOAP and REST Differences

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved