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

What is the use of static class?

0 Answers  


What is final variable?

0 Answers  


What are the benefits of java?

0 Answers  


What is incompatible types in java?

0 Answers  


how to run servlet program between two computer through the internet ?

2 Answers   Kiran Prakashan, TCS,


What is anti pattern in programming?

0 Answers  


What function extracts specified characters from a string?

0 Answers  


How the elements are organized in GridBagLayout?

1 Answers  


which method is used to know the status of the Thread?

9 Answers   Honeywell,


Explain garbage collection in java?

0 Answers  


what is difference between requestprocessor and request dispatcher?

2 Answers   Tech Mahindra,


Class c implements interface I containing method m1 and m2 declarations. Class c has provided implementation for method m2. Can I create an object of class c?

0 Answers  


Categories