Explanation says : On the other hand, if a a separate object of type Comparator is used, the collection of X objects can be sorted in any way depending on what Comparator you use.
How can we be sure that class does not use separate Comparator for sorting?
Since compareTo method is used for sorting that means, Class \\\X\\\ implements \\\Comparable\\\ interface.
Class may not necessary implement Comparable if it has compareTo method, and class may use Comparator as well in this situation.
In if this true - correct answer would be
Class X neither implements Comparable nor Comparator. Object of a separate class that implements Comparator is used for sorting.
The problem statement clearly tells you that the compareTo() method is used while sorting the given collection.
If a Comparator is used to sort, then it would have used compare() method and not compareTo.
If you like our products and services, please help us by posting your review here.
The mechanism used in this situation allows the Objects of class X to be sorted in only one way.
Not true, not at all. You can use the fact that a class implements compareTo by invoking Comparator.reverseOrder() to retrieve a comparator that can be used in Collections.sort(list,comparator) to sort the list in reverse order.
There are questions where we are expected to conceive of things, circumstances, possibilities that are not in the question, and if we do not do so we get it wrong. I do not see why this question is different. It is inconsistent.
Almost everything can be done using other code and classes. The question is about the given situation.
Having said that, the question clearly says that when the list is sorted, compareTo method is used. So, the Comparator interface is not being used here.
Can you show code that will sort the list in any other way using the same compareTo method?
If you like our products and services, please help us by posting your review here.
I truly applaud your effort in proving your point.
The sort method that you have used in your code is defined in List interface (newly added in 1.8), while the question says you have a collection. Even so, since a List is a collection, even one case as shown by your code makes this option incorrect.
I will ask the author to update this question asap.
many thanks!
Paul.
If you like our products and services, please help us by posting your review here.