How do I use the double brace initialization?

Category: fundamental, viewed: 544 time(s).

The double brace initialization {{ ... }} is another way for initializing a collection objects in Java. It is offer a simple syntax for initializing a collection object.

package org.kodejava.example.lang;

import java.util.List;
import java.util.ArrayList;

public class DoubleBraceInitialization {
    public static void main(String[] args) {
        //
        // Creates a list of colors and add three colors of
        // Red, Green and Blue.
        //
        List<String> colors1 = new ArrayList<String>();
        colors1.add("Red");
        colors1.add("Green");
        colors1.add("Blue");


        //
        // Creates another list of colors and add three colors
        // using the double brace initialization.
        //
        List<String> colors2 = new ArrayList<String>() {{
            add("Red");
            add("Green");
            add("Blue");
        }};

        for (String color : colors2) {
            System.out.println("Color = " + color);
        }
    }
}

What's actually happened is: the first brace creates an anonymous inner class and the second brace is an initializer block. Due to the need for creating an inner class the use of double brace initialization is considered to be slower.

Because of this performance issue you it's better not to use this technique for you production code, but using it in your unit testing can make your test looks simpler.

Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats