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 the difference between heap and stack memory?

914


What will be the initial value of an object reference which is defined as an instance variable?

923


Can we make main() thread as daemon?

851


What is the purpose of stub and skeleton?

777


What is field name?

789


What is the basic concepts of OOPS?

940


How to convert string to byte array and vice versa?

810


How do you remove an object from an arraylist in java?

688


How many types of gc are there in java?

770


Difference between static and dynamic class loading.

821


Give me example of derived data types.

862


What is pojo class in java?

744


Can you have two constructors in java?

715


Differences between traditional programming language and object oriented programming language?

838


Can we convert stringbuilder to string in java?

773