How to do this in Java - passing a collection of subtype to a method requiring a collection of base type?
The example below gives:
The method foo(Map<String,List>) is not applicable for the arguments (Map<String,MyList>)
I can implement by creating a class hierarchy for the typed collections - but is it possible otherwise?
public void testStackOverflow() {
class MyList extends AbstractList {
public Object get(int index) {
return null;
}
public int size() {
return 0;
}
};
Map <String, List> baseColl = null;
Map <String, MyList> subColl = null;
foo (subColl);
}
private void foo (Map <String, List> in) {
}
EDIT: Turns out from the answers that this requires "bounded wildcard" so adding this text for searchability