The outer Map
test
uses an upper wildcard value which prevents values being added whereas the Map
temp
does not.
You could use
Map<String, Map<String, String>> test = new HashMap<String, Map<String, String>>();
^ ^
|-----changed LHS type--- must be matched with -|
The bounded wildcard syntax Map<String, ? extends Map<String, String>>
means the Map
uses a value of some unknown type that extends Map
. The compiler prevents all values except null
being added.
Notice also the right hand side value has a Map
type rather than a HashMap
as generics are not covariant which means generic types on the right hand side assigned values must match exactly with those from the left hand side declaration.