HashMap to return default value for non-found keys?

Larry picture Larry · Sep 22, 2011 · Viewed 149.4k times · Source

Is it possible to have a HashMap return a default value for all keys that are not found in the set?

Answer

Spycho picture Spycho · Mar 31, 2014

In Java 8, use Map.getOrDefault. It takes the key, and the value to return if no matching key is found.