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

Is null or empty java?

0 Answers  


where lives in jvm

5 Answers  


What are register variables what are the advantages?

0 Answers  


What are the basic concepts of OOPS in java?

0 Answers   Axtria, ITC Indian Tobacco Company,


what is the purpose of "virtual"?

0 Answers   EDS,


Explain what is Marker interface?

0 Answers   BirlaSoft,


Can we define private and protected modifiers for variables in interfaces?

0 Answers  


What is the frontend and backedn in Java?

3 Answers   TCS,


What is the difference between an inner class and a sub-class?

0 Answers  


What is string [] java?

0 Answers  


What are the two types of java programming?

0 Answers  


How many bytes are a float?

0 Answers  


Categories