interface

when do we use comparator and comparable?.

Your help is kindly appreciated.

Regards

Eugene
eugene007Asked:
Who is Participating?
 
CEHJCommented:
When objects need to be sorted. Implementing Comparable gives the ability for the Collections class to do a default sort on the object in question. A Comparator can provide an non-existent implementation or override one
0
 
CEHJCommented:
In the case of your previous question, Department itself implemented Comparable, meaning that Collections could do a default sort. If you used a Comparator, you could override that implementation and sort by some other criteria
0
 
eugene007Author Commented:
why do we use interface.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
An interface is a way of specifying a behavioural contract between a 'server' and a 'client' without specifying how the behaviour is implemented
0
 
eugene007Author Commented:
so for comparable:

Collections.sort(list); // natural order

provided the compareTo() method is not overriden, otherwise the sorting will be based on criteria.


Collection.sort(list, new Comp())

The compare() method has been overriden. The sorting is base on criteria.



0
 
CEHJCommented:
That's is basically, yes
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.