Double Brace Initialization

In many languages, you can create list literals, which allow you to create a list with elements already pre-defined inside of it.

But what about Java?

List<Integer> list = new ArrayList<Integer>(1, 2, 3, 4);

If you come from any language that supports list literals, you would expect the snippet above to work. Unfortunately, the snippet doesn’t compile.

The alternative and obvious solution would be to write :

List<Integer> list = new ArrayList<Integer>();
list.add(1)
list.add(2)
list.add(3)
list.add(4)

While this approach does work, it has a drawback.
The drawback is that you cannot use anonymous lists as a parameter for a method. Since you can’t initialize and fill a list with data at the same time, it follows that you can’t create an anonymous list and fill it with data either.

public void printList(List<Integer> list){
  for(int i = 0; i < list.size(); i++){
     System.out.println(list.get(i));
  }
}

The printList method can’t be used with an anonymous list. Since you can’t insert any data into an anonymous list, the printList method is completely useless.

The Solution


Double brace initialization allows you to create a List and fill it with data at the same time.

  List<Integer> list = new ArrayList<Integer>() {{
    add(1);
    add(2);
    add(3);
    add(4);
  }};

With double brace initialization, you can now use an anonymous list as input.

  printList(new List<Integer>(){{
    add(1);
    add(2);
    add(3);
    add(4);
  }});
Output :
1
2
3
4

EDIT: The above can be more concisely done with :

printList(Arrays.asList(1,2,3,4));

You can also use for loops with double brace initialization, as well as declare variables inside.

For example :

printList(new ArrayList<Integer>(){{
  add(1);
  add(1);

  int x = 1;
  int y = 1;
  int z = 1;

  for(int i = 0; i < 8; i++){
    x = y;
    y = z;
    z = y + x;
    add(z);
  }
}});

In the code above, we are simply filling a list with the first 10 numbers of the Fibonacci sequence and printing them out.

The results :

1
1
3
5
8
13
21
34

 

How Double Brace Initialization Works


The first set of curly braces creates an inner class for the anonymous list.
The second set of curly braces will create an instance initialization block, which is a block of code that will run before the constructor is called.

Here’s an example of what that looks like :

public class A {
  {
    System.out.println("Hello World!");
  }
}

public class B {
  public static void main(String[] args){
    A a = new A();
  }
}

Output :
Hello World!

So when we do double brace initialization, what we’re really doing is creating a new List, making an inner class with the first set of braces, making an instance initialization block with the second set of braces, and then filling the list with data inside the instance initialization block.

Overall, double brace initialization is a quick and nifty trick that will save you the agony of actually making named lists and manually adding in the defined data one at a time, when you can simply insert an anonymous list with all the defined data instantly.

Advertisement

2 thoughts on “Double Brace Initialization

    • The Arrays.asList tip is a great idea! I actually never thought about that, but it pretty much makes my example obsolete. I’ll make a note that my example was poor and add a different one.

      However, as for the pollution of the namespace issue, it seems to be a bit of a pre-optimization. The performance difference is negligible, so it can effectively be ignored unless you are creating huge amounts of anonymous classes with double brace initialization.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s