Diff between Comparator and Comparable?
Answers were Sorted based on User's Feedback
Answer / qim2010
The “Comparable” interface allows itself to compare with
another similar object (i.e. A class that implements Comparable
becomes an object to be compared with). The following method
are specified in the interface.
public int compareTo(Object o)
public int compareToIgnoreCase(String str)
The "Comparator" interface is used to compare two different
objects. The following method is specified in the Comparator
interface.
public int compare(Object o1, Object o2)
| Is This Answer Correct ? | 8 Yes | 0 No |
Answer / rajesh
Comparable interface uses Object's compareTo() to compare
the objects. We have to override the compareTo() method in
order to sort.
Where as Comparator, checks for compare() method. You can
sort based on particular property of the object.
| Is This Answer Correct ? | 4 Yes | 2 No |
Lowest Common ancestor in a Binary Search Tree and Binary Tree.
Explain the difference between private, public, package and protected in java?
What is qualitative variable?
What are Interceptors?
How to perform merge sort in java?
where do we use init()
what is the difference between thread and runnable types? : Java thread
When should we create our own custom exception classes?
Which method returns the length of a string?
How many ways can you break a singleton class in java?
what is object deep copy and shallow copy and why it is required?
What is the statements?