Converting non-generic List type to Generic List type in Java 1.5

Viewed 19209

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<ObjectType> based on the incoming List object so that my calling code is talking to List<ObjectType>.

What's the best way to convert the List (or any other object collection) to a List<ObjectType>.

5 Answers

The best and safest way is to use java.util.Collections method 'checkedList(List list, Class type)'

With this method, all of the items in your old List will be checked as early as possible.

Related