Java Double Brace Initialization

1.Overview

In this quick tutorial, we’ll show how double braces can be used for creating and initializing objects in a single Java expression.

We’ll also look at why this technique can be considered an anti-pattern.

2.Standard Approach

Normally we initialize and populate a set of countries as follows:

@Test
public void whenInitializeSetWithoutDoubleBraces_containsElements() {
    Set<String> countries = new HashSet<String>();                
    countries.add("India");
    countries.add("USSR");
    countries.add("USA");
  
    assertTrue(countries.contains("India"));
}

As can be seen from above example, we are doing the following:

  1. Create an instance of HashSet
  2. Add countries to the HashSet
  3. Finally, we assert whether the country is present in the HashSet
3.Using Double Brace

However, we can actually combine the creation and initialization in a single statement; this is where we make use of double braces:

@Test
public void whenInitializeSetWithDoubleBraces_containsElements() {
    Set<String> countries = new HashSet<String>() {
        {
           add("India");
           add("USSR");
           add("USA");
        }
    };
  
    assertTrue(countries.contains("India"));
}

As can be seen from above example, we are:

  1. Creating an anonymous inner class which extends HashSet
  2. Providing an instance initialization block which invokes the add
    method and adds the country name to the HashSet
  3. Finally, we can assert whether the country is present in the HashSet
4.Advantages of Using Double Braces

There are some simple advantages of using double braces:

  1. Fewer lines of code compared to the native way of creation and
    initialisation
  2. The code is more readable
  3. Creation initialization is done in the same expression
5.Disadvantages of Using Double Braces

Disadvantages of using double braces are:

  • Obscure, not widely known way to do the initialization
  • It creates an extra class every time we use it
  • Doesn’t support the use of the “diamond operator” – a feature
    introduced in Java 7
  • Doesn’t work if the class we are trying to extend is marked final
  • Holds a hidden reference to the enclosing instance, which may cause
    memory leaks

It’s due to these disadvantages that double brace initialization is considered as an anti-pattern.

6. Alternatives
6.1. Stream Factory Methods

Instead, we can make good use of the new Java 8 Stream API to initialize our Set:


2
3
4
5
6
7
@Test
public void whenInitializeUnmodifiableSetWithDoubleBrace_containsElements() {
    Set<String> countries = Stream.of("India", "USSR", "USA")
      .collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
  
    assertTrue(countries.contains("India"));
}
6.2. Java 9 Collections Factory Methods

Also, Java 9 will bring a set of useful factory methods that will make the following possible:

List<String> list = List.of("India", "USSR", "USA");
Set<String> set = Set.of("India", "USSR", "USA");

You can read more about this in this article.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值