Once a map holds pairs you often need to walk through all of them. A map hands you its keys and its values as two separate collections you already know how to work with.
keySet() returns a Set of every key in the map. Because keys are unique, a set is the natural shape for them.
Map<String, Integer> stock = new Map<String, Integer>{ 'pens' => 12, 'paper' => 4 };
Set<String> names = stock.keySet();
System.debug(names.size()); // 2
values() returns a List of every value. Values may repeat, so a list is used instead of a set.
List<Integer> counts = stock.values(); System.debug(counts.size()); // 2
There is no way to loop over the pairs directly. The standard pattern is a for-each loop over keySet(), reading each value with get():
Integer total = 0;
for (String itemName : stock.keySet()) {
total = total + stock.get(itemName);
}
System.debug(total); // 16
When you only care about the values, loop over values() instead and skip the lookups.
Do not add or remove pairs while looping over keySet(), because changing the map underneath the loop is not safe. Build up a separate collection inside the loop and apply your changes after it finishes.
Summing amounts, finding the largest value, or collecting every key that passes a test are all the same shape of loop. Knowing that a map gives you a Set of keys and a List of values means every skill you learned for those collections works here too.