Java8 introduced those nice methods getOrDefault() and putIfAbsent(), allowing to write code like:
Map>
An important point about computeIfAbsent is that it takes a Function which will only get executed if the Key is absent and we need a default Value.
Whereas getOrDefault requires the default Value itself, already computed. In this case, the default Value we would need is a new ArrayList, which has the side effect of allocating a new object on the heap.
We want to defer doing that until we are sure that the key isn't already in itemsByFoo. Otherwise we would be generating unnecessary garbage for gc to collect.