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 nullpointerexception in java?

0 Answers  


When should you make a function static?

0 Answers  


Is a class subclass of itself?

0 Answers  


how to use finalize()Method to resources

4 Answers   TCS,


Is there any sort function in java?

0 Answers  


What does || || mean in math?

0 Answers  


Are true and false keywords?

0 Answers  


What is array initialization in java?

0 Answers  


java is puerly object oriented or not ?

5 Answers  


What is inner class?what is the use of inner class?where we create the object for inner class? and inner class can extend any class or inner class can implement any interface?

1 Answers   Wipro,


What is casting?

3 Answers  


how to print a numbers from 1 to 100 with out using control structures?

4 Answers   IBM,


Categories