I have a couple of questions about generic wildcards in Java:
What is the difference between List extends T>
and List super
In general,
If a structure contains elements with a type of the form
? extends E
, we can get elements out of the structure, but we cannot put elements into the structure
List ints = new ArrayList();
ints.add(1);
ints.add(2);
List extends Number> nums = ints;
nums.add(3.14); // compile-time error
assert ints.toString().equals("[1, 2, 3.14]");
To put elements into the structure we need another kind of wildcard called Wildcards with super
,
List