Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. **What is the difference between Comparator and Comparable**
  2. ___
  3.  
  4.  
  5. | Comparable interface | Comparator interface |
  6. | ------------ | ------------ |
  7. | Present in **java.lang package** |Present in **java.util package** |
  8. | Defines one important method i.e. <br> public int **compareTo**(Object obj); |Defines 2 method i.e.; <br>public int **compare**(Object obj1, Object obj2);<br> public boolean **equals**(Object object);|
  9. | It is basically used for default **natural sorting order** | This is preferred for **customized sorting order** |
  10. | This interface need to be implemented in the same class for which sorting is required | Separate class is required to implement Comparator interface |
  11. | Elements of List can be sorted using comparable interface using sort(); method of Collections class, as shown below<br>**Example**: Collection.sort(listItems); | Elements of List can be sorted using comparator interface using sort(); method of Collections class, as shown below<br>**Example**: Collection.sort(listItems, comparator);|
  12. | String & wrapper classes’ like Integer, Double, etc implement comparable interface | There are very few classes’ which implements Comparator interface|
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement