HashMap to return default value for non-found keys?

后端 未结 14 2118
别跟我提以往
别跟我提以往 2020-11-27 14:05

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

14条回答
  •  有刺的猬
    2020-11-27 14:09

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

提交回复
热议问题