This course is currently a work in progress. Please join the waitlist to receive regular updates.
Accessing keys and values from a map
To access all the keys from a map, use the keySet()
method and to find all values from a map, use the values()
method.
- keySet()
Use the
keySet()
method to iterate through all keys in a map. For example, in a map of products and discounts, you can use keySet() to loop through all discounted products. - values()
Use the
values()
method to iterate through all values in a map. For instance, to find the maximum discount among all products, iterate through the values of the map and compare the discounts for each product to identify the one with the highest discount.
Loading...
In the above example:
- The map
productDiscountMap
contains the products and discounts. - The
keySet()
method returns the keys[Iphone, Ipad, Mac]
as a set from the map. - The
values()
method returns the values as a list[20, 30, 15]
from the map.
Try It Yourself
Loading...