EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Top Differences Tutorial Comparable vs Comparator

Comparable vs Comparator

Yashi Goyal
Article byYashi Goyal
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated March 20, 2023

Comparable vs Comparator

Difference Between Comparable and Comparator

In the following article, we will discuss the difference between Comparable vs Comparator. We first need to understand what are they and why are they used? Comparing the two values like 2 integers, 2 float values are easy in Java programming language using simple arithmetic operators, but how can we compare the values of 2 objects like two Students, two Employees, etc. Comparing the objects of Collections in Java is a little bit difficult and can be done by simple arithmetic and comparison operators. For the comparison of Collection objects in Java, the Comparable and Comparator interfaces are used. Comparable and Comparator are the two interfaces present in 2 different Java packages to sort the collection of objects. By implementing these interfaces in the class, we can use the various methods of them.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What is Comparable?

The comparable interface has a single method compareTo() which is used to compare the value of an object with the one supplied in the method. The Comparable interface defines the natural or default ordering of the objects of a class.

On defining the compareTo() method in the class, we need to make sure that the return value can be:

  • Negative: Returns negative if the object which is to be compared is less than the supplied object value.
  • Positive: Returns positive if the object which is to be compared is greater than the supplied object value.
  • Zero: Returns zero if the object which is compared is equal to the supplied object value.

What is Comparator?

Comparator interface is used for the scenarios when we don’t want to use the natural ordering and want the ordering/ sorting of elements according to the specific requirements. As we cannot make any changes to the compareTo() method of Comparable interface, the Comparator interface provides a method compare() which is used to sort the objects of collection.

It takes 2 arguments in the method, and the return value can be:

  • Negative: Returns a negative value if the first argument’s value is less than the value of the second argument.
  • Positive: Returns a positive value if the first argument’s value is greater than the value of the second argument.
  • Zero: Returns a zero value if the first argument’s value is equal to the value of the second argument.

Head to Head Comparison between Comparable and Comparator (Infographics)

Below are the top 6 differences between Comparable vs Comparator:

Comparable vs Comparator info

Key Difference between Comparable and Comparator

Some of the key differences between the Comparable interface and Comparator interface are given below:

  1. Comparator interface provides multiple ways of sorting the objects of Collections, whereas Comparable interface provides only a single way of sorting the objects.
  2. The comparable interface needs to be implemented by the class if it needs to use its methods, whereas, for Comparator, nothing needs to be done in the original class.
  3. Basically, Comparable is used for the objects with natural ordering; for example, in an Employee class, Employees need to be sorted on the basis of their Employee_id, whereas in the Comparator interface, sorting needs to be done through a separate class.
  4. Only a single object is passed as an argument in a Comparable interface, whereas 2 arguments are passed in the Comparator interface, which means a Comparable interface compares this reference with the value of the object specified in the method, whereas in Comparator comparison is done with 2 different objects provided in an argument of the method.
  5. Comparable is provided by default as it is present in java.lang package, whereas Comparator is provided as a utility and is present in java. util package.
  6. There is no need to use Arrays.sort(), Collections.sort() method if the class is implementing the Comparable interface as it automatically sorts the objects of Array and List using compareTo() method, whereas it is not so in the case of Comparator interface.
  7. Using Comparators allows us to not to add extra code in the original class, whereas using Comparable, mandatorily, we need to add the code.
  8. The comparable interface allows us only a single sorting sequence, which means we can sort the collection on the basis of a single element like in an Employee table, sorting can be done on a single basis like name, employee id, date of joining, etc. whereas Comparator interface provides multiple sorting sequence which means we can sort the collections on multiple bases like name, employee id, date of joining, etc.
  9. If the user does not have access to the original code and needs to write only the sorting function, Comparable can not be used for those scenarios, and Comparator is the only choice for such cases.

Comparable vs Comparator Comparison Table

Let’s discuss the top comparison between Comparable vs Comparator:

Comparable

Comparator

In order to sort the objects of collections, Comparable provides a compareTo() method to the programmers. In order to sort the objects of collections, the Comparator provides compare() method to the programmers.
Using Comparable, we can sort the elements of collection on the basis of a single element. Using Comparator, we can sort on the basis of multiple elements of the collections at a time.
A comparable interface is present in java.lang package Comparator interface is present in java. util package
Sorting in Comparator interface is based on natural order, for example, roll numbers of students in a Student class. Sorting in a Comparable interface is done through a separate class.
Using Comparable, the original class using it is affected, i.e.modification is done on the actual class. Using Comparator, the original class is not affected, i.e. modification is not done on the actual class.
The syntax for sorting the list  using Comparator is Collections.sort(List) The syntax for sorting the list using Comparator is Collections.sort(List, Comparator)

Conclusion

Collections are one of the most important and used sections while programming in Java language. So understanding its every concept deeply is very important be it sorting, searching, printing on a console, etc. The above description clearly explains what Comparable and Comparator interface is, why is it used for and the major difference between the two of them. As both are used in their specific scenarios, before using any of them in a program, the programmer must understand the situation thoroughly.

Recommended Articles

This is a guide to Comparable vs Comparator. Here we discuss the key differences with infographics and comparison table. You can also go through our other suggested articles to learn more –

  1. Google Cloud vs AWS
  2. Comparable  in Java with Example
  3. GitHub vs SVN
  4. Z Score vs T Score
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Software Development Bundle5000+ Hours of HD Videos | 149 Learning Paths | 1050+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more