0% found this document useful (0 votes)
34 views2 pages

Package: Parameter Comparable Comparator

Comparable is used for natural ordering where the sorting logic is in the same class as the objects. Comparator allows for different sorting based on attributes by implementing the sorting logic in a separate class. With Comparable, the class of the objects must implement the interface, while with Comparator a different class can implement the interface to sort based on another class's attributes. Both CompareTo and Compare return integers to indicate if an object is greater than, equal to, or less than the compared object.

Uploaded by

Hemanth Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Package: Parameter Comparable Comparator

Comparable is used for natural ordering where the sorting logic is in the same class as the objects. Comparator allows for different sorting based on attributes by implementing the sorting logic in a separate class. With Comparable, the class of the objects must implement the interface, while with Comparator a different class can implement the interface to sort based on another class's attributes. Both CompareTo and Compare return integers to indicate if an object is greater than, equal to, or less than the compared object.

Uploaded by

Hemanth Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Parameter 1.

Package

Comparable Java.lang.Comparable.

Comparator Java.util.Comparator.

2. Sorting logic

Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects.

Sorting logic is in separate class. Hence we can write different sorting based on different attributes of objects to be sorted. E.g. Sorting using id,name etc..

3. Implementation

Class whose objects to be sorted must implement this interface.e.g Country class needs to implement comparable to collection of country object by id.

Class whose objects to be sorted do not need to implement this interface.Some other class can implement this interface. E.g.CountrySortByIdComparator class can implement Comparator interface to sort collection of country object by id. int compare(Object o1,Object o2) This method compares o1 and o2 objects. and returns a integer.Its value has following meaning. 1. positive o1 is greater than o2 2. zero o1 equals to o2 3. negative o1 is less than o1 .

4. Sorting method

int compareTo(Object o1) This method compares this object with o1 object and returns a integer.Its value has following meaning 1. positive this object is greater than o1 2. zero this object equals to o1 3. negative this object is less than o1.

5. Calling method

Collections.sort(List) Here objects will be sorted on the basis of CompareTo method .

Collections.sort(List, Comparator) Here objects will be sorted on the basis of Compare method in Comparator.

You might also like