Wildcard versions are preferred. If the parameter is of type List<?> , Then it is clear that any List will be accepted. If any List accepted, there is no reason to specify a parameter of type name, so the <E> record will be just messy. On the other hand, if a type parameter appears twice in the signature, you cannot use wildcards. For example, a type parameter is required for this signature.
public static <E> List<E> combineLists(List<E> list1, List<E> list2)
Actually, in this example, it would be better if the arguments were of type List<? extends E> List<? extends E> (the only way to do this without wildcards is to have three type parameters, complete mess).
In Efficient Java, it is recommended that even if a type parameter is needed in the method body, you should prefer a wildcard version of the signature and write a private helper method to make this possible. For instance:
public static void swapFirstAndLast(List<?> list) { helper(list); } private static <E> void helper(List<E> list) { int size = list.size(); E e = list.get(0); list.set(0, list.get(size - 1));
source share