Get value of key from a mutable map

1.1k views Asked by At

I have a Map which looks as follows:

 val myMap = mutableMapOf<String,String>() 

 myMap.put("A", "APPLE")
 myMap.put("B", "BALL")
 myMap.put("C", "CAT")
 myMap.put("D", "DOG")
 ...

When I print the map as follows, it prints it correctly:

println("MAP DATA -> $myMap")

//Output
MAP DATA -> [A = APPLE, B = BALL, C = CAT, D = DOG, ...]

But in my activity when I want to check if the value for a specific key is what it is, so that I can check a specific radio button, it doesn't work.

I tried the following:

 val option1 = myMap.get("A")
        
 if(option1 == "APPLE"){

    radioOption1.isChecked = true

  }

EDIT: The 2nd way I tried was this (in this case, the println text gets printed, but the radio button won't get checked.:

    if(myMap.getValue("A") == "APPLE"){
        println("getValue of MAP")
        radioOption1.isChecked = true
    }

When I try this it says : The key 'A' does not exist in the map.

I am struggling with it. How can I get the value of a specific key from the map so that I can check the relevant radioButton?

Any help or advice will be highly appreciated.

0

There are 0 answers