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

Why stringbuilder is not thread safe in java?

559


Can we declare a class as abstract without having any abstract method?

547


What is the purpose of the runtime class in java programming?

553


What variables are stored in stack?

521


What is the differences between c++ and java? Explain

578






Where are variables stored?

546


what is comparable and comparator interface?

567


Can one thread block the other thread?

598


What is math in java?

556


Explain the private field modifier?

603


What is double data type?

534


What is the difference between a break statement and a continue statement?

587


Why string is called as immutable?

518


Can static methods access instance variables in java?

565


What is meant by stack and queue?

592