On page 140 of Effective Java, we recommend that the method signature with a wildcard is preferable to one with a type parameter that appears only once. For instance,
public static void swap(List<?> list, int i, int j)
preferable
public static <T> void swap(List<T> list, int i, int j)
However, it is not possible to set the List<?> Element in any way (other than null ), so effective Java suggests writing a private helper method to make a wildcard signature.
private static <T> void swapHelper(List<T> list, int i, int j) { list.set(i, list.set(j, list.get(i))); } public static void swap(List<?> list, int i, int j) { swapHelper(list, i, j); }
However, I looked at the source code for Collections.swap and found that the way they encountered the List<?> Problem was to use raw types.
public static void swap(List<?> list, int i, int j) { final List l = list; l.set(i, l.set(j, l.get(i))); }
We are not encouraged to use raw types in new code (except for instanceof checks). So, I would like to know what is the reason? Is there a performance advantage when using the source types here, rather than calling a private helper method? If not, are there examples where using raw types can improve performance?