Difference between Comparable and Comparator - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
Difference between Comparable and Comparator

Difference between Comparable and Comparator

Difference between Comparable and Comparator

Comparable and Comparator both are interfaces and can be used to sort collection elements.
But there are many differences between Comparable and Comparator interfaces that are given below.
ComparableComparator
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.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) Comparable affects the original class i.e. actual class is modified.Comparator doesn't affect the original class i.e. actual class is not modified.
3) Comparable provides compareTo() method to sort elements.Comparator provides compare() method to sort elements.
4) Comparable is found in java.lang package.Comparator is found in java.util package.
5) We can sort the list elements of Comparable type by Collections.sort(List)method.We can sort the list elements of Comparator type by Collections.sort(List,Comparator) method.

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.