Home > DeveloperSection > Forums > Difference between comparable and comparator in java
Sanjay Sharma
Sanjay Sharma

Total Post:45

Points:315
Posted on    July-28-2015 7:20 AM

 Java Java  Collection 
Ratings:


 1 Reply(s)
 566  View(s)
Rate this:
What is  the difference between Comparable and comparator on java


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    July-28-2015 8:55 AM

Comparable
1) Comparable provides single sorting sequence. In other words, we can sort the collection on the basis of single element such as id or name or price etc.
2) Comparable affects the original class i.e. actual class is modified.
3) Comparable provides compareTo() method to sort elements.
4) Comparable is found in java.lang package. .
5) We can sort the list elements of Comparable type by Collections.sort(List) method.

Comparator
1)Comparator provides multiple sorting sequence. In other words, we can sort the collection on the basis of multiple elements such as id, name and price etc.
2)Comparator doesn't affect the original class i.e. actual class is not modified.
3) Comparator provides compare() method to sort elements.
4) Comparator is found in java.util package
5) We can sort the list elements of Comparator type by Collections.sort(List,Comparator) method.

Don't want to miss updates? Please click the below button!

Follow MindStick