Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
java

What is double brace initialization in Java?

Sumit Sharma

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Double brace initialization

Double brace initialization is a technique to create and initialize an object within a single expression. Generally, when we create an object of any Collection (ArrayList, HashMap, HashSet etc.) we consume 2-3 lines on declaration and initialization. Whenever we use a collection, we need to do the following:

  • First, we have to create a variable of that collection.
  • Then, we initialize a collection object.
  • We reference the object in the variable and add elements to it.
  • After performing these actions, we can use that collection.

We can use double brace initialization to avoid these actions.

import java.util.*;

class Solution {
   public static void main(String[] args) {
        // Traditional Approach
        // Declaration and initialization of Arraylist
        ArrayList<Integer> list= new ArrayList<>();
        // add elements to the list
        list.add(1);
        list.add(2);
        list.add(3);
        //use the arrayList to getSum
        int x = getSum(list);
        System.out.println("X: " + x);
       //declaration, initialization and adding the 
       // element within same expression
        ArrayList<Integer> list1= new ArrayList<Integer>(){{
            add(1);
            add(2);
            add(3);
        }};
        //use the arraylist to getSum
        int y = getSum(list1);
        System.out.println("Y: " + y);

        //declaration , initialization and adding the 
        // elements and using it within same expression
        int z = getSum(new ArrayList<Integer>(){{
            add(1);
            add(2);
            add(3);
        }});
        System.out.println("Z: " + z);
    }
    static  int getSum(ArrayList<Integer> arr){
        int sum=0;
        for(int i:arr)
            sum+=i;
        return sum;
    }
}

Explanation

  • In line 1 we import java.util.* to use Java collections.

  • In lines 7 to 11 we declare the Arraylist object list, initialize it, and add values in the list.

  • We then call the getSum() function and store the returned value in variable x.

  • In lines 17 to 21 we use double brace initialization to create an arraylist object, list1, in a single expression.

  • We then use that arraylist in the next expression in line 22.

  • In lines 28 to 32 we create an arraylist, add values to it, and use the arraylist as a parameter for the same getSum() function in a single expression.

  • In lines 35 to 39 the logic of the getSum() function is given.

RELATED TAGS

communitycreator
java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring