In collection sorting comparable and comparator interface will be used..but why this two interfaces required..two will work same purpose so why there are two interfaces instead of one?when to use comparator and when to use comparable?



In collection sorting comparable and comparator interface will be used..but why this two interfaces ..

Answer / obi

In order to implement an interface (such as Comparable) you need access to rewrite a specific class.
If a certain class is given to you as a 'black box' and you now need some way to use a written sort on it - notice that its a black box thus you cannot rewrite any of its code - you would have to create a new class which would act as a Comparator.

Is This Answer Correct ?    6 Yes 0 No

Post New Answer

More Core Java Interview Questions

Explain tree set and its features?

0 Answers  


What is a line separator in java?

0 Answers  


What is the difference between add() and addElement() method in Vector Class ?

2 Answers   TCS,


how do I create a runnable with inheritance? : Java thread

0 Answers  


JVM is platform independent or depeneded?

7 Answers  






Can a class declared as private be accessed outside it’s package?

0 Answers  


why would you use a synchronized block vs. Synchronized method? : Java thread

0 Answers  


What is unsigned char?

0 Answers  


Explain what pure virtual function is?

0 Answers   Aricent,


What are the supported platforms by java programming language?

0 Answers  


What are the differences between checked exception and unchecked exception?

0 Answers  


What is an eror in java?

0 Answers  


Categories