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?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you mean by ternary operator in java?

560


When will you define a method as static?

560


What do you mean by platform independence of Java?

538


Why null value is used in string?

530


If an object is garbage collected, can it become reachable again?

546






What happens if an exception is throws from an object's constructor?

630


What is the point of java?

554


how to know the total memory occupied by the objects in the ArrayList(Array list may contain duplicate objects)

1907


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

548


Why is serialization required?

543


What do you mean by platform independence? What is an interface?

565


Difference between method overloading and method overriding in java ?

574


Can we have multiple public classes in a java source file?

558


What does the “static” keyword mean? Can you override private or static method in java?

633


What exactly is methodology?

486