Efficiency of Java “Double Brace Initialization”?

In Hidden Features of Java the top answer mentions Double Brace Initialization, with a very enticing syntax:

Set flavors = new HashSet() {{




    add("butter pecan");


This idiom creates an anonymous inner class with just an instance initializer in it, which "can use any [...] methods in the containing scope".

To create sets you can use a varargs factory method instead of double-brace initialisation:

public static Set setOf(T ... elements) {

    return new HashSet(Arrays.asList(elements));


The Google Collections library has lots of convenience methods like this, as well as loads of other useful functionality.


As for the idiom's obscurity, I encounter it and use it in production code all the time. I'd be more concerned about programmers who get confused by the idiom being allowed to write production code.