Implementierung Hashmap mit unterschiedlichen Wert-Typen in Kotlin
Ist es möglich, eine hashmap in Kotlin, nimmt unterschiedliche Werte-Typen?
Habe ich versucht, dieses:
val template = "Hello {{world}} - {{count}} - {{tf}}"
val context = HashMap<String, Object>()
context.put("world", "John")
context.put("count", 1)
context.put("tf", true)
... aber das gibt mir ein type mismatch (offenbar "John"
, 1
und true
sind nicht Objekte)
In Java können Sie dies umgehen, indem erstellen von Typen new String("John")
, new Integer(1)
, Boolean.TRUE
ich habe versucht, den Gegenwert in Kotlin, aber noch immer der Fehler.
context.put("tf", Boolean(true))
Irgendwelche Ideen?
InformationsquelleAutor Jan Vladimir Mostert | 2016-05-26
Du musst angemeldet sein, um einen Kommentar abzugeben.
In Kotlin,
Any
ist der obertyp aller anderen Typen, und Sie sollten ersetzt JavaObject
:InformationsquelleAutor hotkey