This course is currently a work in progress. Please join the waitlist to receive regular updates.

Adding elements to a map

To add a new element to a map, use the Put() method. The put method takes two parameters, the first parameter is the key and the second parameter is the value.

Following is an example of a map which contains products and its prices.

Loading...

In the above example:

  • The keyword Map is used to declare a map which can hold key value pairs.
  • The key is a string and value is an integer.
  • Using the put() method, the key and values are inserted into the map.

Note


  • Map keys are case-sensitive; "iphone" and "Iphone" will be treated as separate keys.
  • A Map key cannot have duplicate values. If you add a duplicate key, the latest value will overwrite the previous one.

Try It Yourself

Loading...