The .getOrDefault() method solves the null problem you saw with .get(). Instead of returning null when a key is missing, it returns a fallback value you provide:
int age = ages.getOrDefault("Dave", 0); // 0 if Dave is absent
This is safer than .get() because it eliminates the NullPointerException risk when you unbox into a primitive. It also makes your code shorter. Without it, you would need an if check or a ternary expression.
The method does not add the default value to the map. After calling .getOrDefault("Dave", 0), the key "Dave" still does not exist. If you need to insert a default when a key is missing, look into .putIfAbsent() instead.