Maps (Collections) in kotlin
package com.example.my_kotlin_practice fun main (args: Array<String>) { // Kotlin map is a collection of key/value pairs, where each key is unique, and it can only be associated with one value. // The same value can be associated with multiple keys though. We can declare the keys and values to be any type; // there are no restrictions. // A Kotlin map can be either mutable (mutableMapOf) or read-only (mapOf). // Maps are also known as dictionaries or associative arrays in other programming languages val theMap = mapOf ( "one" to 1 , "two" to 2 , "three" to 3 , "four" to 4 ) println (theMap) val theMutableMap = mutableMapOf ( "one" to 1 , "two" to 2 , "three" to 3 , "four" to 4 ) println (theMutableMap) // Creating Map using HashMap : A Kotlin map can be created from Java's HashMap. val theMap2 = HashMap<String, Int>() theMap2[ "one" ] = 1...
.png)
.png)
.png)
Comments
Post a Comment