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 10, 2018 in Java by misc.edu04
• 1,430 points
155 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 10, 2018 by code.reaper12
• 3,450 points

Related Questions In Java

0 votes
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,540 points
1,252 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,190 points
69 views
0 votes
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,640 points
555 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 in Java by Neha
• 6,280 points
56 views
+5 votes
3 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,380 points

edited Nov 6, 2018 by Omkar 11,087 views
0 votes
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,110 points
2,397 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,110 points
6,197 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,110 points
768 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 19, 2018 in Java by code.reaper12
• 3,450 points
1,020 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,450 points
122 views