Other type of Map

fun main() {
    val map = mutableMapOf(
        1 to "One",
        2 to "Two",
        3 to "Three"
    )

    println("Original Map: $map")

    map.put(4, "Four")
    println("Map after adding element: $map")

    map.remove(2)
    println("Map after removing element with key 2: $map")

    println("Check if key 3 exists: ${map.containsKey(3)}")
    println("Check if value 'One' exists: ${map.containsValue("One")}")

    map.clear()
    println("Map after clearing all elements: $map")
}

Explanation:

  • mutableMapOf creates a mutable map with initial key-value pairs.
  • put adds a new key-value pair to the map.
  • remove removes the element associated with the given key from the map.
  • containsKey checks if the map contains a specific key.
  • containsValue checks if the map contains a specific value.
  • clear removes all elements from the map, making it empty.