Tuesday 21 April 2020

Kotlin : Collections Map & HashMap (Post 17)

Hi ,

Again I am here to share one another topic in Kotlin series i.e. Map & HashMap

So as we all know that how map & hashmap algorithm , how they save the elements using keys .
In the same way here in Kotlin , the elements are saved using keys , also elements modifications are also done using keys.

Now , after talking so much for this , let's directly move to technical stuff :

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
       
        //immutable , fixed size , Read only operation
        var myMap = mapOf<Int, String>(13 to "Android", 4 to "iOS", 32 to "Flutter")
        for (key in myMap.keys) {
            println("Elements with Key: $key => ${myMap[key]}")
        }

        //mutable ,not fixed in size ,Read & Write Operations
        var mutableMap1 = hashMapOf<Int, String>()
        var mutableMap2 = HashMap<Int, String>()
        var mutableMap = mutableMapOf<Int, String>()
        mutableMap.put(13, "android")
        mutableMap.put(3, "iOS")
        mutableMap.put(6, "flutter")

        //remove
        mutableMap.remove(3) //removing
        mutableMap.put(3, "React Native") //adding
        for (key in mutableMap.keys) {
            println("mutable Elements with Key: $key => ${mutableMap[key]}")
        }
    }

So here we can see that , I have added two types of maps one is immutable and another one is mutable .

In both types of maps , I have added the elements on the basis of keys
and in mutable type of maps I have also modified the elements using keys.

That's a small demo foe how maps and hashmaps will work with Kotlin. In next post I will share the functionality of  Set & HashSet , till then Happy Coding 😀😀

No comments:

Post a Comment

Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...