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

  • International Labour's Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई
    International Labour’s Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई General Knowledge
  • JSP Implicit Objects
    JSP Implicit Objects JSP
  • Gautama Buddha life story in Hindi, Buddha's history
    Gautama Buddha life story in Hindi, Buddha’s history Biography
  • JSON The Data Interchange Standard Format
    JSON The Data Interchange Standard Format Important
  • Java Hibernate Framework Tutorial
    Java Hibernate Framework Tutorial | Hibernate For Beginners Hibernate
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography
  • Difference between final, finally and finalize
    Difference between final, finally and finalize Differences
  • Pointers in C Programming, Definition, Types & Example
    Pointers in C Programming, Definition, Types & Example C Language

Difference between Comparable and Comparator

Posted on August 16, 2021October 27, 2021 By admin 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.

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 Interface and Abstract class Difference between Interface and Abstract class Differences
Difference between TCP and UDP Difference between TCP and UDP Differences
Difference between GenericServlet and HttpServlet Difference between GenericServlet and HttpServlet Differences
JDBC Driver in Java JDBC Driver 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

Related Posts

  • Difference between Interface and Abstract class
    Difference between Interface and Abstract class Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP Differences
  • Difference between GenericServlet and HttpServlet
    Difference between GenericServlet and HttpServlet Differences
  • JDBC Driver in Java
    JDBC Driver 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

Leave a Reply Cancel reply

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

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • International Labour's Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई
    International Labour’s Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई General Knowledge
  • JSP Implicit Objects
    JSP Implicit Objects JSP
  • Gautama Buddha life story in Hindi, Buddha's history
    Gautama Buddha life story in Hindi, Buddha’s history Biography
  • JSON The Data Interchange Standard Format
    JSON The Data Interchange Standard Format Important
  • Java Hibernate Framework Tutorial
    Java Hibernate Framework Tutorial | Hibernate For Beginners Hibernate
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography
  • Difference between final, finally and finalize
    Difference between final, finally and finalize Differences
  • Pointers in C Programming, Definition, Types & Example
    Pointers in C Programming, Definition, Types & Example C Language
  • 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