HashMap: Updation and Removal
Let's discuss the update and deletion operations in HashMap.
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.
Get hands-on with 1400+ tech skills courses.