Detail about the "super" wildcard in java generics
generics, java, super
Solution
Map<? super String, ? super String> mappa1 = new HashMap<Object,Object>();
Since Java Generics are based on type erasure, with this line you didn't create a `MashMap<Object,Object>`. You just created an instance of the `HashMap` class; the type parameters get lost immediately after this line of code and all that stays is the type of your `mappa1` variable, which doesn't even mention `Object`. The type of the `new` expression is assignment-compatible with the type of `mappa1` so the compiler allows the assignment.
In general, the type parameters used with `new` are irrelevant and to address this issue, Java 7 has introduced the diamond operator `<>`. All that really matters is the type of `mappa1`, which is is `Map<? super String, ? super String>`; as far as the rest of your code is concerned, this is the type of the instantiated map.
Problem
I have a question regarding generics: ``` Map<? super String, ? super String> mappa1 = new HashMap<Object,Object>(); ``` with super it's possible to instantiate a `HashMap<Object,Object>` for a `<? super String>`. However then you can add only objects which extends String ( in this case only String itself). Why don't they forbid by compilation error as well as happens with the `extends` wildcard. I mean if once created a `Map <Object, Object>` it's possible only to add Strings.. why not forcing to create a `Map<String, String>` in the first place? (like it happens with the `extends` wildcard) Again I know the difference between `super` and `extends` concerning generics. I would like just to know the details I have aboved-mentioned. Thanks in advance.