In Java 8, I am trying to cast Map<String, ? extends Object>
to Map<String, Object>
. I though that it would be safe given the constraint I put on the input Type (all ? must implement Object), but I get an unchecked cast warning.
Any idea where I reason wrong ? Any clean solution ? Thanks for your help !
This cast is not safe. In particular:
Map<String, ? extends Object> before;
before.put("foo", "example"); // <-- illegal
Map<String, Object> after;
after.put("foo", "example"); // <-- legal
Observe that String
is not a subclass of ? extends Object
because you don't know ?
but it is a subclass of the more general Object
thus the second call is okay.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With