Groovy - put()


Advertisements

Associates the specified value with the specified key in this Map. If this Map previously contained a mapping for this key, the old value is replaced by the specified value.

Syntax

Object put(Object key, Object value)

Parameters

  • Key – The key to be put in the map
  • Value – The associated value for the key

Return Value

The returned key-value pair which is inserted.

Example

Following is an example of the usage of this method −

class Example { 
   static void main(String[] args) { 
      def mp = ["TopicName" : "Maps", "TopicDescription" : "Methods in Maps"] 
      mp.put("TopicID","1");
      println(mp); 
   } 
}

When we run the above program, we will get the following result −

[TopicName:Maps, TopicDescription:Methods in Maps, TopicID:1]
groovy_maps.htm
Advertisements