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

Retrieving a value by key

To retrieve a value from the map, use the get() method, which takes the key as its argument.

Below is an example of a map that contains products and their prices, and how to use the get() method to retrieve the price of 'MacBook'.

Loading...

In the above example:

  • The keyword Map is used to declare a map which can hold key value pairs.
  • The key is string and value is an integer.
  • The get() method retrieves the value associated with a key. If the key doesn't exist, it returns null.

Note


A map key can hold the null value

Try It Yourself

Loading...