Map API Improvements: Fetch Operations

This lesson explains the new methods added in Map API for fetch operations.

If you have used Map then you must have faced a challenge where you needed to update the value of a key in the Map. Now, before updating, you must first check if the value is present in the Map, get the current value, update it, and again put the value in the Map. This is quite a cumbersome process, and it involves using lots of if/else statements. This kind of code is difficult to understand and fix if any issues occur.

Thankfully, Java 8 has introduced some new methods in the Map interface to make our lives easier. In this lesson, we will discuss some of those new methods.

1. getOrDefault()

This method is a lifesaver if you need to update a certain key in the Map. The getOrDefault() method either returns the value of the key, or it returns the default value if the key is not present.

Get hands-on with 1400+ tech skills courses.