I have seen this question on SO that looks to convert a Map<String, Object>
to a Map<String, String>
. It tries to do a downcasting of the value of the map.
I have read the answers and it is required to iterate the map, downcast the value and put it in the new map (with different variations).
I am trying to do the opposite: I want to upcast the value. I want to convert a Map<String, SomeClass>
to a Map<String, SomeUpperClass>
, where SomeClass extends SomeUpperClass
but I am having the same problem as the question I mentioned before.
Compiler tells me:
incompatible types: java.util.Map< java.lang.String, SomeClass> cannot be converted to java.util.Map< java.lang.String, SomeUpperClass>
I understand that downcasting can be dangerous and error prone, but I don't understand why I have to iterate the whole map to upcast values to put them in a new map.