Comparable :
Comparator:
In some situations, you may not want to change a class and make it comparable. In such cases,
The method required to implement is compare(). Now let's use another way to compare those TV by size. One common use of
In brief, a class that implements
A class that implements
Example :
More info at : Click Here
Comparable
is implemented by a class in order to be able to
comparing object of itself with some other objects. The class itself
must implement the interface in order to be able to compare its
instance(s). The method required for implementation is compareTo()
.Comparator:
In some situations, you may not want to change a class and make it comparable. In such cases,
Comparator
can be used if you want to compare objects based on certain
attributes/fields. For example, 2 persons can be compared based on
`height` or `age` etc. (this can not be done using comparable.)The method required to implement is compare(). Now let's use another way to compare those TV by size. One common use of
Comparator
is sorting. Both Collections
and Arrays
classes provide a sort method which use a Comparator
.In brief, a class that implements
Comparable
will be comparable, which means it instances can be compared with each other. A class that implements
Comparator
will be used in mainly two situations: 1) It can be passed to a sort method, such as Collections.sort()
or Arrays.sort()
,
to allow precise control over the sort order and 2) It can also be used
to control the order of certain data structures, such as sorted sets
(e.g. TreeSet
) or sorted maps (e.g., TreeMap
).Example :
class Dog implements Comparator<Dog>, Comparable<Dog>{ private String name; private int age; Dog(){ } Dog(String n, int a){ name = n; age = a; } public String getDogName(){ return name; } public int getDogAge(){ return age; } // Overriding the compareTo method public int compareTo(Dog d){ return (this.name).compareTo(d.name); } // Overriding the compare method to sort the age public int compare(Dog d, Dog d1){ return d.age - d1.age; } } public class Example{ public static void main(String args[]){ // Takes a list o Dog objects List<Dog> list = new ArrayList<Dog>(); list.add(new Dog("Shaggy",3)); list.add(new Dog("Lacy",2)); list.add(new Dog("Roger",10)); list.add(new Dog("Tommy",4)); list.add(new Dog("Tammy",1)); Collections.sort(list);// Sorts the array list for(Dog a: list)//printing the sorted list of names System.out.print(a.getDogName() + ", "); // Sorts the array list using comparator Collections.sort(list, new Dog()); System.out.println(" "); for(Dog a: list)//printing the sorted list of ages System.out.print(a.getDogName() +" : "+ a.getDogAge() + ", "); } }
Parameter
|
Comparable
|
Comparator
|
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.
|
Implementation
|
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 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 |
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 | |
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 |
Package
|
Java.lang.Comparable
|
Java.util.Comparator
|
No comments:
Post a Comment