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 Dictionary? and what purpose it is used for?

3 Answers  


What access modifiers can be used for class ?

0 Answers  


In what circumstances, compiler will supply a default constructor for a class?

4 Answers  


What is the public method modifier?

0 Answers  


How to create a base64 decoder in java8?

0 Answers  






What is Distributed Application and what is its usage?

2 Answers  


Can you access the private method from outside the class?

0 Answers  


What is string [] java?

0 Answers  


Name some classes present in java.util.regex package.

0 Answers  


What happens if an exception is throws from an object's constructor?

0 Answers   Amazon,


What does int [] mean in java?

0 Answers  


What is command line argument in java?

0 Answers  


Categories