As per Java API spec, the signature of Collections.reverseOrder is
public static
And the example gi
If you look at the signatures for Arrays.sort()
you will see that the one you are calling has a signature of Arrays.sort(T[], Comparator super T>)
, so you are in effect supplying a type parameter in the form of the array type you pass in.
EDIT: I made a small mistake here, and assumed that the call to reverseOrder()
could infer the type, but without at least one parameter, no method could actually do that (see the link).
If the call to Collections.reverseOrder()
took a parameter, then the method could infer the type from the call. There is an overloaded version of the method that takes a Comparator
as an argument. We can demonstrate the inference in Eclipse with the following; hover over Collections.reverseOrder()
:
public class CollectionsFun {
public static void main(String[] args) {
String[] strs = new String[] {"foo", "bar", "baz"};
Comparator comp = new Comparator() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
};
Arrays.sort(strs, Collections.reverseOrder(comp));
System.out.println(strs);
}
}
Because the call to reverseOrder()
in this example takes a typed parameter, the static method can infer the type that needs to be returned. However, when you call the overloaded form of the method that takes no parameter, no type can be inferred, so you get a Comparator
back instead of a Comparator
. You can see this by hovering over the reverseOrder()
call in the code below:
public class CollectionsFun {
public static void main(String[] args) {
String[] strs = new String[] {"foo", "bar", "baz"};
Arrays.sort(strs, Collections.reverseOrder());
System.out.println(strs);
}
}
As the signature to Arrays.sort()
says it takes a Comparator super T>
it matches Comparator
. So this all compiles without giving you a "raw types" warning anywhere.
Finally, if you want to show how this would work without type inference, you would do the following:
public class CollectionsFun {
public static void main(String[] args) {
String[] strs = new String[] {"foo", "bar", "baz"};
Arrays.sort(strs, Collections.reverseOrder());
System.out.println(strs);
}
}
The type parameter supplied above ensures that the type of the Comparator
that gets returned is Comparator
.