Fun Facts: Double Brace Initialization
Java has many detractors and one of the most common gripes is that you can’t create a List, Map, Collection, etc. with an initial set of values. As with most other hate-speech directed at Java, this is not true! There is a handy feature colloquially called double brace initialization. It is used as follows:
As you can see it’s clean and particularly useful for the Map interface. The way it works is:
- The first pair of braces create an anonymous class
- The second pair of braces create an instance initialization block, which allows any supported Map functions to be run against it.
If you’re thinking, “Geez wiz! This is too good to be true!!” Well you’re not the only one, and as you may expect, there is a significant drawback to this method. Every time you use this method, the JVM creates a new class. This means that if you run this type of method in a loop of 1000 iterations, 1000 NEW .class files will be created. A poster on StackOverflow ran an experiment with this method vs. the standard filling the Collection after it is initialized method. The result, creating 3 ArrayLists, came to roughly 190ms for double brace initialization and 0ms for the standard way. This is not good.
Do not fear, there is yet hope for the future, in Java 7. One of the proposed changes is to add the ability to initialize Collections as they are declared:
There’s a saying around the office that goes something like, “Thanks Josh Bloch!” These new alterations to the Collections package definitely fall into the that category. Looking forward to Java 7!