Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   RUBY   
   C++   




PUT() - FUNCTION


The put() method is used to insert a new element(i.e. A Key Value pair) in the Map.


Example :



fun main() {

    var x = mutableMapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language")
    
    x.put("Rose", "Is a Flower")
    println(x)
}


Output :



  {5=Is a Number, John=Is a Name, Kotlin=Is a Language, Rose=Is a Flower}

So, in the above code we have created a Map using mutableMapOf() and Key and Value pairs.


var x = mutableMapOf(5 to "Is a Number", "John" to "Is a Name", "Kotlin" to "Is a Language")

And initialised to the variable x.

java_Collections

Now, we are supposed to add a new entry to the Map. Where the Key would be Rose and the Value Is a Flower.


So, we have used the put() method to add it.


x.put("Rose", "Is a Flower")

And the entry is added to the Map.

java_Collections

And we get the below output,

Output :



  {5: 'Is a Number', 'John': 'Is a Name', 'Kotlin': 'Is a Language', 'Rose': 'Is a Flower'}