September 2019
Intermediate to advanced
816 pages
18h 47m
English
Before JDK 8, the solution to this problem relied on a helper method, which basically checks the presence of the given key in a Map and returns the corresponding value, or a default value. Such a method can be written in a utility class or by extending the Map interface. By returning a default value, we avoid returning null if the given key was not found in the Map. Moreover, this is a convenient approach for relying on a default setting or configuration.
Starting with JDK 8, the solution to this problem consists of a simple invocation of the Map.getOrDefault() method. This method gets two arguments representing the key to look up in the Map method and the default value. The default value acts as the backup value ...