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 is meant by encapsulation?

845


What is the length of a string?

743


What is java util concurrentmodificationexception?

710


What is stringreader?

709


How many types of keywords are there?

766


Is java a compiler?

756


Can we overload the constructors?

756


What are different types of encoding?

826


Can a class be final?

706


we have syntax like for(int var : arrayName) this syntax is to find whether a number is in the array or not.but i want to know how to find that number's location.

1808


What are the files generated after using IDL to java compiler?

820


Can we sort list in java?

798


Why main() method is public, static and void in java ?

844


Does anyone still use java?

843


Can we declare a constructor as final?

913