First let's reduce the problem by using Set
s instead:
Set<Set<Long>> longSetSet = null;
Set<Set<Double>> doubleSetSet = null;
Set<Set<? extends Number>> someNumberSetSet;
// try assigning them
someNumberSetSet = longSetSet; //
someNumberSetSet = doubleSetSet; // compiler errors - incompatible types
At first glance you might wonder why this assignment is illegal, since after all you can assign a Set<Long>
to Set<? extends Number>
The reason is that generics are not covariant. The compiler prevents you from assigning a Set<Set<Long>>
to Set<Set<? extends Number>>
for the same reason it won't let you assign a Set<Long>
to a Set<Number>
. See the linked answer for more details.
As a workaround, you can use a type parameter in your method signature as other answers have suggested. You can also use another wildcard to make the assignment legal:
Set<? extends Set<? extends Number>> someNumberSetSet;
someNumberSetSet = longSetSet; //
someNumberSetSet = doubleSetSet; // legal now
Or in your example:
function(Map<Integer, ? extends Map<Integer, ? extends Number>> arg) { }