Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Difference between Comparator and comparable in Collections

807591Apr 22 2008 — edited Apr 22 2008
i have been going through the collections chapter and i understand that to sort a collection(list or map or set)we need to use the Collections interface which has a compareTo method that takes in the list that we need to sort as its argument and for this we need to make the class in which we create the list to implement comparable interface and implement the compareTo method.
This is as per my understanding my doubt is there is a line in the book im reading(SCJP 5 by kathy serra)that the difference between Comparable and Comparator is that

Comparator interface you can use it to sort instances of any class�even classes you can't modify�
unlike the Comparable interface, which forces you to change the class whose instances you want to sort.

i dint understand this sentence and what do they exactly mean by classes you cant modify...please clarify...thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 20 2008
Added on Apr 22 2008
6 comments
459 views