If I have the following code:
public class DummyClass<T> {
public List<T> getList() {
return new ArrayList<T>();
}
public Set<List<T>> getListSet() {
return new HashSet<List<T>>();
}
}
and I have a DummyClass<?> dummy,
I can do
List<?> list = dummy.getList();
without any errors.
However,
Set<List<?>> listSet = dummy.getListSet();
gives the compile error:
Type mismatch: cannot convert from Set<List<capture#1-of ?>> to Set<List<?>>
for the line assigning dummy.getListSet().
Why can't I assign dummy.getListSet() to a Set<List<?>>?
First is important to know, that the compiler replace ? operator with Object class. So when you write this:
your class definition will look like this:
and methods like this:
You can write
but not