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
What is a default method?
What kind of variables can a class consist?
What is contract between hashcode and equal method?
What does void * mean?
What is byte data type?
What is a package in java? List down various advantages of packages.
how many types of Inheritance?
What is the difference between superclass and subclass?
Can arraylist contain null values?
What is string array?
What state does a thread enter when it terminates its processing in java programming?
Can you sort a string in java?
What are the basics of core java?
Is static a keyword in java?
What do you mean by collectors in java 8?