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>.
When inter-operating with legacy code that doesn’t specify type parameters for generic types, use a wildcard. For example, suppose you are calling a method in an older library that simply returns a raw
Collection:In your code, assign the result to a variable declared with a wildcard:
This way, you preserve type safety so you won’t get any warnings.
The legacy code might specify (in a comment, most likely) what the generic parameters should be. For example:
In this case, you have a choice. You can cast the result to a
Collection<Item>, but if you do so, you are relying 100% on the third-party library, and discarding the assurance of Java generic types: that anyClassCastExceptionraised at runtime will occur right at an explicit cast.What if you don’t fully trust the third-party library, but still need to produce a
Collection<Item>? Then create a new collection, and add the contents after casting them to the expected type. That way, if there is a bug in the library, you find out about it right away, rather than having some code far away and much later mysteriously blow up with aClassCastException.For example:
For a case where the type parameter isn’t known at compile-time, you can use the type-checked collection factories of the
Collectionsclass.