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

0 Answers  


Can we use catch statement for checked exceptions when there is no chance of raising exception in our code?

0 Answers  


What is a variable analysis?

0 Answers  


can any one tell me what is advantage of encapsulation

2 Answers  


Is 0 an irrational number?

0 Answers  






What is the basic functionality of DataOutput interface in java?

2 Answers   TCS,


What one should take care of, while serializing the object?

0 Answers  


Can we create our own daemon thread?

0 Answers  


What are the four integer types supported by java?

0 Answers  


1) There are 10 different threads in runnable state. Each having priority 1 to 10. How does the CPU schedules or executes these threads?

2 Answers   RBS,


What is the difference between final, finally and finalize()?

0 Answers  


Can we use different return types for methods when overridden?

0 Answers  


Categories