Implement Hashmap with different value types in Kotlin
For new visitors,It can also be done with this
val a= hashMapOf<Any,Any>( 1 to Exception(), 2 to Throwable(), Object() to 33)
Where both keys and values can be of any type.
In Kotlin, Any
is the supertype of all the other types, and you should replace Java Object
with it:
val context = HashMap<String, Any>()
context.put("world", "John")
context.put("count", 1)
context.put("tf", true)