In Hidden Features of Java the top answer mentions Double Brace Initialization, with a very enticing syntax:
Set flavors = new HashSet
This will call add()
for each member. If you can find a more efficient way to put items into a hash set, then use that. Note that the inner class will likely generate garbage, if you're sensitive about that.
It seems to me as if the context is the object returned by new
, which is the HashSet
.
If you need to ask... More likely: will the people who come after you know this or not? Is it easy to understand and explain? If you can answer "yes" to both, feel free to use it.