Map API Improvements: Replace Operations

In the previous lesson, we discussed a few new methods that have been added to the Map interface. In this lesson, we will look at some more improvements that have been done in Map API.

1. replace(), replaceAll(), and remove()

Sometimes we are required to change certain values from Hashmap. Before Java 8, the only way to do this was to iterate over the Map and change each value one by one.

This is a cumbersome process, and it is prone to errors if the logic is not written properly. To overcome this issue, a few new methods have been introduced in Java 8.

a) replace(K key, V value)

This method replaces the entry for the specified key only if it is currently mapped to some value. If the key is not present or if the key is present but the current value is null, then nothing is done.

Get hands-on with 1200+ tech skills courses.