In Python, the defaultdict class provides a convenient way to create a mapping from key -> [list of values], in the following example,
from collections import defaultdict
d = defaultdict(list)
d[1].append(2)
d[1].append(3)
# d is now {1: [2, 3]}
Is there an equivalent to this in Java?
Why I chose Tendayi Mawushe's solution
This solution is good because it more faithfully renders the idea of defaultdict. However, you're not quite recovering the Pythonic expressiveness -- defaultdict is actually taking a function, not a class (below). On the other hand, that would probably make the Java code using it more verbose
> python
>>> def f(): return x
>>> x = 3
>>> from collections import defaultdict
>>> y = defaultdict(f)
>>> y[2]
3
Sorry for the latency accepting this, and thanks to Luno and dfa for their helpful answers as well.
postscript -- if only Scala compiled faster, I would use it instead...