In Java, how do I convert List<?> to List<T> using a general purpose method so that I can replace patterns like the following with a single method call:
List untypedList = new ArrayList(); // or returned from a legacy method
List<Integer> typedList = new ArrayList<Integer>();
for (Object item: untypedList)
typedList.add((Integer)item);
Note that the above code does not generate any type-safety warnings and, ideally, your solution shouldn't generate any such warnings, either.
Will the following solution work provided that list Class<L> has a public default constructor?
public class ListUtil {
public static <T, L extends List<T>> L typedList(List<?> untypedList, Class<T> itemClass, Class<L> listClass) {
L list = null;
try {
list = listClass.newInstance();
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
for (Object item: untypedList)
list.add(itemClass.cast(item));
return list;
}
}
(Note that listClass.newInstance() throws InstantiationException or IllegalAccessException if an instance of Class<L> does not have a public default constructor. What problems may arise if the method does not properly handle these exceptions?)
Notes:
Tis the type of each item in the resulting list.Lis the type of the list that I wish to create (which extendsList<T>).untypedListis the "untyped" input list, effectively the same asList<Object>.itemClassrepresents the runtime class ofT.listClassrepresents the runtime class ofL.