Diff between Comparator and Comparable?




Answers were Sorted based on User's Feedback



Diff between Comparator and Comparable?..

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

Diff between Comparator and Comparable?..

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

Post New Answer




More Core Java Interview Questions

How many types of methods are there in java?

0 Answers  


What is the difference between constructor and method?

4 Answers  


What is the purpose of finalization in java programming?

0 Answers  


What is the life cycle of Servlet?

3 Answers  


What is super keyword in java ?

1 Answers  






What is multiple inheritance & can we implement it in java?

6 Answers   Satyam,


Is an integer an object?

0 Answers  


Name and explain the types of ways which are used to pass arguments in any function in java.

0 Answers  


Does variable declaration allocate memory?

0 Answers  


What is time complexity algorithm?

0 Answers  


How do you make a thread in java?

0 Answers  


How to avoid the runtime exception ?

2 Answers  






Categories