I am trying to implement a generic sort utility method for a List of objects of any class that implements MyInterface. Per Java API (http://java.sun.com/javase/6/docs/api/java/util/Collections.html), Collections.sort() method signature is:
public static <T> void sort(List<T> list, Comparator<? super T> c)
I am not sure if List with a wildcard parameter can substitute a "plain" parametrized List, but I tried:
static void mySort(List<? extends MyInterface> myList, SortCriteria mySortCriteria) {
Collections.sort(myList, new Comparator<? super MyInterface>() {
...
});
}
and got a compile time error,
The type new Comparator(){} cannot extend or implement Comparator<? super MyInterface>
A supertype may not specify any wildcard.
So, I changed it to:
static void mySort(List<? extends MyInterface> myList, SortCriteria mySortCriteria) {
Collections.sort(myList, new Comparator<MyInterface>() {
...
});
}
And it compiles and works. Any good explanations?