Fetching an element from a HashMap

There are two ways to get an element from a HashMap.

Using the get() method

The get(Object key) method takes a key as a parameter and returns the value corresponding to that key. If the key is not present, it returns null.

Using the getOrDefault() method

The getOrDefault(Object key, V defaultValue) method is useful if are not sure whether a key is present in the Map or not. If the key is present then this method returns the value corresponding to the key and if the key is not present then the default value is returned.