In this tutorial you will learn about HashMap via simple step by step examples.

Example 1: HashMap

Let us look at a simple HashMap example written in Kotlin Programming Language. Follow the following steps:

Step 1: Create Project

  1. Open your favorite Kotlin IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Add Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following Kotlin files:

Kotlin Android SharedPreferences

Kotlin Android SharedPreferences
Kotlin Android SharedPreferences
  • HashMap.kt
  1. In your editor or IDE, create a file known as HashMap.kt.
  2. Then add the following code:

(a). HashMap.kt

package Collections.HashMaps

fun main(args : Array<String>) {
    val dict = hashMapOf (
            "adam" to "Illionis",
            "bryan" to "Alabama",
            "cain" to "Oklahoma"
    )

    println("Current location ${dict["adam"]}")

    //this is called multi declaration where you assign the each pair key and value to separate variables
    for ((k, v) in dict)
        println("Key in $k has value $v")

    for (k in dict.keys)
        println("Key in '$k' with value '${dict[k]}'")

    println("Looping using entrySet")

    for (n in dict.entries)
        println("Key ${n.key} with value ${n.value}")
}

Step 4: Run

Copy the code, build and run.

Reference

Here are the reference links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License