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,380 points
74 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,520 points
492 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
37 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,320 points
201 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
99 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,360 points

edited Nov 6, 2018 by Omkar 4,551 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,030 points
1,218 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,030 points
3,496 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,030 points
217 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
312 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
45 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.