Comparable Vs Comparator
Both are used to sort the objects of the class.
Packages:
Comparable is in: java.lang
Comparator ...
When -1: this returned that means: this object’s id is less than emp.id
When 1: this returned that means: this object’s id...
Calling method

negative – this object is less than
o1
Collections.sort(List)
Here objects will be sorted on the
basis of ...
Upcoming SlideShare
Loading in …5
×

Comparable vs comparator

176 views

Published on

Brief description about Comparable vs comparator

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
176
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Comparable vs comparator

  1. 1. Comparable Vs Comparator Both are used to sort the objects of the class. Packages: Comparable is in: java.lang Comparator is in: java.util Syntax: Interface Comparable<T> T- the type of object that this object may be compared to Interface Comparator<T> T- the type of object that this object may be compared to Class implementing Comparable interface need to override int compareTO(T o) which compares this obj with specified object for order. Class implementing Comparator interface need to override int compare(T o1, T o2) compares its two arguments for order. boolean equals(Object o) indicates whether some other object is "equal to" this comparator. Comparable When we create list(or array) of object of user defined class, we cannot call Collection.sort() method to sort object in the list, because they do not have natural ordering. To define natural ordering we implement Comparable interface on the Object we want to compare our class to. Implementing class must override compareTo(Object o) method Example: class Employee implements Comparable<Employee> { int id; String name; int compareTo(Employee emp) { return (this.id<emp.id) ? -1 : (this.id>emp.id) ? 1 : 0; } }
  2. 2. When -1: this returned that means: this object’s id is less than emp.id When 1: this returned that means: this object’s id is greater than emp.id When 0: both objects are equal. OR return this.id.compareTo(emp.id); This will return result in ascending order. To print in descending order: return –this.id.compareTo(emp.id); // - negative sign returns decending Comparator The class whose objects to be sorted do not need to implement this interface. Some third class can implement this interface to sort. E.g., EmployeeSortByIdComparator class can implement Comparator interface to sort collection of employee object by id. Using Comparator interface, we can write different sorting based on different attributes of objects to be sorted. 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 must implement this interface. e.g. Employee class needs to implement comparable to collection of country object by id Sorting method int compareTo(Object o1) This method compares this object with o1 object and returns a integer. Its value has following meaning positive – this object is greater than o1 zero – this object equals to o1 Class whose objects to be sorted do not need to implement this interface. Some other class can implement this interface. E.g.EmployeeSortByIdComparator 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. positive – o1 is greater than o2
  3. 3. Calling method negative – this object is less than o1 Collections.sort(List) Here objects will be sorted on the basis of CompareTo method zero – o1 equals to o2 negative – o1 is less than o1 Collections.sort(List, Comparator) Here objects will be sorted on the basis of Compare method in Comparator

×