Say I have the following two classes:
public class SomethingElse<A, B> {
public List<? extends Something<A, B>> getOneList() {
//doesn't matter
}
public List<? extends Something<A, B>> getAnotherList() {
//doesn't matter
}
}
public class Something<A, B> {
//doesn't matter
}
I would like to merge the results that I get from getOneList()
and getAnotherList()
:
SomethingElse<String, Integer> somethingElse = new SomethingElse<>();
List<? extends Something<String, Integer>> oneList = somethingElse.getOneList();
List<? extends Something<String, Integer>> anotherList = somethingElse.getAnotherList();
anotherList.forEach(e -> oneList.add(e)); //<-- DOESN'T COMPILE
However, the compiler complains that in the .add()
method above, it is expecting capture of ? extends Something<String, Integer>
but I am providing... well, capture of ? extends Something<String, Integer>
:
I feel this has something to see with type erasure but I can't figure out why, even the compiler itself is unable to produce a clear message since it's telling me I'm providing type X but it's expecting type X.
Can anyone explain me technically why the compiler doesn't like this? What are the possible wrong mixes I may be doing into the same list?