Reputation: 249
class Demo {
<T> void gTee(List<List<T>> tl) {
tee(tl);
}
void tee(List<List<?>> tl) {
}
}
JDK 8 says
incompatible types: java.util.List<java.util.List<T>> cannot be converted to java.util.List<java.util.List<?>>
How come? I was of the opinion that the ?
wildcard stands for any type.
Upvotes: 4
Views: 106
Reputation: 249
Ah. Holy cube. I should have known but it did not come to my mind:
It compiles now:
class Demo {
<T> void gTee(List<List<T>> tl) {
tee(tl);
}
void tee(List<? extends List<?>> tl) {
}
}
Upvotes: 4
Reputation: 270850
This is due to this behaviour of Java generics:
Even if
A
andB
are compatible types,SomeType<A>
is not compatible withSomeType<B>
A classic example of this is trying to assign a List<Cat>
to a List<Animal>
.
The same thing happens here. Normally, List<T>
can be assigned to List<?>
. But since you are assigning List<List<T>>
to List<List<?>>
, you can't.
Upvotes: 4