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

  • What is Tense in Hindi (काल क्या है)?
    What is Tense in Hindi (काल क्या है)? Grammar
  • Gautama Buddha life story in Hindi, Buddha's history
    Gautama Buddha life story in Hindi, Buddha’s history Biography
  • Subhas Chandra Bose Biography in Hindi, Essay, Paragraph
    Subhas Chandra Bose Biography in Hindi, Essay, Paragraph Biography
  • Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti
    Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti Spiritual
  • Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा
    Vat Savitri Vrat in Hindi, Vat Savitri Puja | वट सावित्री पूजा Festival
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
    Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth Biography
  • Newton's laws of Motion, State and Explained, Formula
    Newton’s laws of Motion, State and Explained, Formula Science
  • P V Sindhu Biography in Hindi
    P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति Biography

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

Java 11 new Features (With example Programs) Java 11 new Features (With example Programs) Important
Exception Handling in Java Exception Handling in Java Java
Java Keywords Java Keywords Java
Difference between SOAP and REST Difference between SOAP and REST Differences
Difference between SQL and HQL Difference between SQL and HQL | Hql vs Sql performance Differences
Difference between Internet and Intranet Difference between Internet and Intranet 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 | द्रौपदी मुर्मू की जीवनी
  • 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
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • 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
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • 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