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

What are meta-annotations?

0 Answers   Cyient,


How to re-get an object that is collected by garbage collector?

1 Answers  


What is the difference between static and global variables and also define what are volatile variables?

0 Answers   Flextronics, Hexaware,


Which of the classes will have more memory allocated?

0 Answers  


What is the % operator?

2 Answers  






How do you achieve polymorphism in java?

0 Answers  


Hi am an mca graduate . i have done bsc maths in my degree . every company asks me why you make shift from maths group to computere field . What i need to answer?

2 Answers  


how to convert mm/dd/yy to dd/mm/yy using collections in java.

4 Answers   Bosch,


Suppose i have two threads t1 and t2 are running.How the main thread will know that the two threads t1,t2 execution has completed?

2 Answers   Subex,


Why java is not a pure object oriented language?

5 Answers   IBM,


What is the destroy method?

0 Answers  


Hi Friends, can u give me Real Time example for interface and abstract class.(With Banking Example)

4 Answers  


Categories