Java map.get(key) - automatically do put(key) and return if key doesn't exist?

后端 未结 7 1162
你的背包
你的背包 2020-12-04 18:44

I am sick of the following pattern:

value = map.get(key);
if (value == null) {
    value = new Object();
    map.put(key, value);
}

This ex

相关标签:
7条回答
  • 2020-12-04 19:44

    Maybe I'm not seeing the whole problem, but how about using inheritance or composition to add this behavior to the Map object?

    0 讨论(0)
提交回复
热议问题