https://kotlinlang.org logo
Title
f

frogger

05/19/2020, 10:55 AM
overriding this java method, how can I add a new item to the map?
override fun extractAuthentication(map: MutableMap<String, *>)
e.g. when I want to add a list the compiler says: “Required Any? found List<String>”
d

diesieben07

05/19/2020, 11:22 AM
MutableMap<String, *>
means: A map with string values and I don't know what the values are. If you don't know what the values are, you cannot safely put anything in, because you cannot know if it would fit.
☝️ 1
Someone could pass in a
MutableMap<String, String>
. Then you cannot add a
List<String>
to that.
f

frogger

05/19/2020, 11:55 AM
But I cannot declare/overwrite it differently, can I?
d

diesieben07

05/19/2020, 12:06 PM
No, you can't, but you cannot safely put stuff into this map
You say this is a Java method? Whats teh declaration on the Java side?
m

Michael de Kaste

05/20/2020, 8:07 AM
@frogger probably needs a MutableMap<String, Any?> not a MutableMap<String, *> The former implies you can add anything, the latter implies you can only add the one type we don't know (and thus you can't add anything)