2

Noob to Kotlin. I have a hashmap and which would hold an array for one of it's keys. However when I read the value for that key, Kotlin is not recognizing it as an array.

My hashmap:

var myHashMap = hashMapOf("test" to arrayOf<HashMap<String, Any>>())

Reading the array:

var testString = "__ ${myHashMap["test"].count()} __"

I am getting type mismatch error when I try to read the value. I am storing the array in hashmap in incorrect way?

My hashmap is of type HashMap. I am just specifying the type for the value now and will be storing the actual values dynamically later.

So later when I read myHashMap["test"], I would be expecting something like ["Hello": "World", "ABC": 3]

Edit: Adding my solution

I tried this and it worked for now, but checking if there is a better solution.

    var tests = task["test"] as ArrayList<HashMap<String, Any>>
    var testCount = tests.count()

Also if I want to keep adding values to myHashMap["test"] for now I am storing the existing values into var, adding new onto it and then passing it out to myHashMap["test"].

tests.add(someHashMap)
myHashMap["test"] = tests

Any quicker way to achieve this?

3 Answers 3

1

By type mismatch, are you referring to the following error?

error: only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Array<kotlin.collections.HashMap<String, Any> /* = java.util.HashMap<String, Any> */>?

If so, you should change the expression to "__${myHashMap["test"]?.count()}__" or "__${myHashMap["test"]!!.count()}__" as myHashMap["test"] can evaluate to null.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want myHashMap["test"] to return ["Hello": "World", "ABC": 3], this should be a map. A way of entering it can be:

mapOf("test" to mapOf("Hello" to "World", "ABC" to 3))

That might also be the reason for your type mismatch error. When defining it as above, the result would be:

var testString = "__ ${myHashMap["test"]!!.count()} __" // -> 2

hashMapOf("test" to arrayOf<HashMap<String, Any>>()) would result into something like:

{
  "test": [
    { "Hello": "World" },
    { "ABC": 3 }
  ]
}

While mapOf("test" to mapOf("Hello" to "World", "ABC" to 3)) would result in something like this:

{
  "test": {
    "Hello": "World",
    "ABC": 3
  }
}

As background: "Hello" to "World" is an entry of a map. You can add multiple ones in mapOf which are then concatenated to a complete may. Your code would look like you would build an array of maps of which each one just has a single entry.

WRT your update: if you want to have a map in the map, you can also write it like this:

myHashMap["test"] = mapOf("Hello" to "World", "ABC" to 3)

If you want to add keys later on, you should also use mutableMapOf instead. Otherwise myHashMap["newTests"] = ... wouldn't work.

Comments

0

In the example you mentioned here, var testString = "__ ${myHashMap["test"].count()} __"

You are getting error because myHashMap["test"] could be null and in that case .count() would throw NullPointerException.

Example - Here you created the hashmap with key "test" and tried to access the same. Try running this -

println(myHashMap["dummy"]) // Output - null

Since kotlin is null safe, if the object is nullable, it is necessary to one of the following null safe assertions.

  1. !! -> This means that you don't care even if object is null and you still want .count() to be called.

Example - myHashMap["dummy"]!!.count() Result here would be NullPointerException

  1. ? -> This means do not want to call count() if in case myHashMap["dummy"] returns null.

Example - myHashMap["dummy"]?.count() Result here would be null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.