To answer your second question, yes, you can cast the List<?>
as a List<Object>
or a List<T>
of any type, since the ?
(Wildcard) parameter indicates that the list contains a homogenous collection of an any Object
. However, there's no way to know at compile what the type
is since it's part of the exported API only - meaning you can't see what's being inserted into the List<?>
.
Here's how you would make the cast:
List<?> wildcardList = methodThatReturnsWildcardList();
// generates Unchecked cast compiler warning
List<Object> objectReference = (List<Object>)wildcardList;
In this case you can ignore the warning because in order for an object to be used in a generic class it must be a subtype of Object
. Let's pretend that we're trying to cast this as a List<Integer>
when it actually contains a collection of String
s.
// this code will compile safely
List<?> wildcardList = methodThatReturnsWildcardList();
List<Integer> integerReference = (List<Integer>)wildcardList;
// this line will throw an invalid cast exception for any type other than Integer
Integer myInteger = integerRefence.get(0);
Remember: generic types are erased at runtime. You won't know what the collection contains, but you can get an element and call .getClass()
on it to determine its type.
Class objectClass = wildcardList.get(0).getClass();