I have a List that is guaranteed to contain just one type object. This is created by some underlying code in a library that I cannot update. I want to create a List
If you just cast to List
in any old place you will get an "unchecked" compiler warning. We resolved that by moving it to a utility method.
public class Lists {
@SuppressWarnings({"unchecked"})
public static List cast(List> list) {
return (List) list;
}
}
Caller now gets no warning, e.g.:
for (Element child : Lists.cast(parent.getChildren())) {
// ...
}
That checkedList
utility is in theory a great idea, but in practice it sucks to have to pass in the class you expect. I hope Java will get runtime generic typing information eventually.