import java.util.*;
class
test implements Comparator<test>
{
private int x;
test(int input) { x = input; }
public static void main(
String args[] )
{
List list = new ArrayList();
list.add(new test(2));
list.add(new test(2));
Collections.sort(list);
}
public int compare( test t1 , test t2 )
{
return t1.x - t2.x;
}
}
The explanation for the above program.
This code will throw a ClassCastException. This version of Collections.sort() banks on the Comparable interface being implemented, not the Comparator interface. No comparator is passed to the sort() method.
I couldn't understand the above explanation.Please explain.