1

How to add some value in ArrayList which is inside of HashMap? My bellow code showing 0 sizes of the hash

val hash= HashMap<String, ArrayList<String> >()
    hash["bro"]?.add("Ali Umar")
    hash["sis"]?.add("Tamanna")
    hash["bro"]?.add("Faruk")
    hash["sis"]?.add("Aklima")
    hash["bro"]?.add("Ab Siddik")
    Log.d("Hash", hash.size.toString())
0

2 Answers 2

3

You have to initialize a List and put the key and that initialized List into the HashMap before you can add any more items to the value of a key. In your example code nothing is put into the HashMap and nothing can be added.

Try it like this (or similar)

fun main() {
    // initialize the hashmap
    val hash = hashMapOf<String, MutableList<String>>()
    // put the keys with empty lists into the hashmap
    hash.put("bro", mutableListOf())
    hash.put("sis", mutableListOf())
    // add items to the value (the list) of existing keys
    hash.get("bro")?.add("Ali Umar")
    hash.get("sis")?.add("Tamanna")
    hash.get("bro")?.add("Faruk")
    hash.get("sis")?.add("Aklima")
    hash.get("bro")?.add("Ab Siddik")
    // print size and content
    println("Hash size is ${hash.size.toString()}")
    println(hash)
}

In the Kotlin Playground this outputs

Hash size is 2
{sis=[Tamanna, Aklima], bro=[Ali Umar, Faruk, Ab Siddik]}
Sign up to request clarification or add additional context in comments.

2 Comments

Many many thanks, bro!. I have also a problem. I want a map like a c++ (stl library's map) in kotlin. But in kotlin hashmap work just as a reference. for example, if I set a value to Hashmap from a variable and then change the value of that variable, the Hashmap value also changed 😒. But I don't want it. I want just like as c++ stl map. How to I dot it? Also advance thanks bro.
Thanks bro, I got the solution of above problem by using .toMutableList()
3

The problem is that the following instructions are just reading from the hashmap but not inserting anything

hash["bro"]
hash["sis"]

so when you create your hashmap with val hash= HashMap<String, ArrayList<String> >() it is empty and "bro" and "sis" do not exist. so it is null and the add function will not be called because of ?. skips execution if the value is null.

so to add something to bro and sis you first have to put values to your hashmap.

hash.put("bro",ArrayList<String>())
hash.put("sis",ArrayList<String>())

this would change your example as follows

val hash= HashMap<String, ArrayList<String> >()
    hash.put("bro",ArrayList<String>())
    hash.put("sis",ArrayList<String>())
    hash["bro"]?.add("Ali Umar")
    hash["sis"]?.add("Tamanna")
    hash["bro"]?.add("Faruk")
    hash["sis"]?.add("Aklima")
    hash["bro"]?.add("Ab Siddik")
    Log.d("Hash", hash.size.toString())

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.