Comparable vs Comparator in Java

0 votes

I need to sort a list of objects based on a particular field. In order to do so, I have developed a program which implements Comparator and works completely fine. But my question is if I use Comparable in this case, will it work? And if it does, then, what is the difference between comparable and comparator interfaces, when one should be used?

May 11, 2018 in Java by misc.edu04
• 1,450 points
1,054 views

1 answer to this question.

0 votes

I think you are not alone who is having this doubt. Even I had this doubt for long, so I will try to answer this question in the simplest way possible.

Between Comparable and Comparator interfaces, you need to remember their sorting order.

If you want your objects to be sorted based on the natural ordering using a single element, then you should go for Comparable interface. While using Comparable, keep this in mind that it directly makes changes in the original class. Basically, it compares the objects of the same class.

Whereas Comparator is used when you want some specific sort order. Using Comparator you can achieve multiple sorting sequences. It doesn’t affect the original class and basically compares the objects, different classes.

Hope, now you have a clear picture of the difference between Comparable and Comparator.

answered May 11, 2018 by code.reaper12
• 3,500 points

Related Questions In Java

+1 vote
1 answer

concat() vs “+” operator : In Java for String concatenation

Basically, there are two important differences between ...READ MORE

answered Apr 27, 2018 in Java by Rishabh
• 3,620 points
4,751 views
0 votes
1 answer

using comparator for sorting in java

You've got two problems: 1) You're using Collections.sort (which takes ...READ MORE

answered Jul 6, 2018 in Java by samarth295
• 2,220 points
797 views
+1 vote
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
3,749 views
0 votes
1 answer

What is Comparable in Java?

As the name itself suggests, Comparable is an interface which defines ...READ MORE

answered Jul 15, 2019 in Java by Neha
• 6,300 points
857 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,243 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,365 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,443 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,882 views
0 votes
1 answer

revalidate() vs repaint() in Java Swing

You need to call repaint() and revalidate() both in order ...READ MORE

answered Sep 20, 2018 in Java by code.reaper12
• 3,500 points
11,134 views
0 votes
1 answer

notify() vs. notifyAll() in Java

notify() is used to wake any thread in the wait ...READ MORE

answered Sep 20, 2018 in Java by code.reaper12
• 3,500 points
2,060 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP