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 an example of character?

0 Answers  


What do u mean by wrapper Class? What do u mean by Jvm... How do u change JVM for other OS? Or No need to Change ...? its like tricky

6 Answers   Accenture, EDS,


Can vector have duplicates in java?

0 Answers  


If set accepts only one argument then how can it compare two objects to avoid duplicates

5 Answers  


What is integer valueof?

0 Answers  






In C we use only compiler. Why java uses both compiler and interpreter? What is its significance?

5 Answers  


What state does a thread enter when it terminates its processing in java programming?

0 Answers  


Can anonymous class have constructor?

0 Answers  


What are the important features of Java 8 release?

0 Answers  


what is enumset?

0 Answers  


What do you mean by collectors in java 8?

0 Answers  


What is the smallest package in Java API?

5 Answers   iGate,


Categories