Home > Java > javaTutorial > body text

How to use double brace initialization in Java

WBOY
Release: 2023-04-18 11:13:02
forward
1317 people have browsed it

By chance, I saw a way to initialize an object:

    // 新建一个列表,并赋值 "Harry","Tony","Tom"
    ArrayList<String> friends = new ArrayList<String>() {{
        add("Harry");
        add("Tony");
        add("Tom");
    }};
Copy after login

Of course, the same initialization method is also used for the Map collection:

    // 新建一个Map,并赋值
    Map<String, Object> cat = new HashMap<String, Object>() {{
        put("name", "Tom");
        put("age", 10);
    }};
Copy after login

Here, internal class syntax is used, this method It is much more convenient and concise than adding new objects first and then adding them one after another. This method is called "double brace initialization" (double brace initialization).

Understanding of this method

Take the initialization of ArrayList as an example. The first layer of curly braces first defines an anonymous internal class inherited from ArrayList

    ArrayList<String> friends = new ArrayList<String>() {
        // 这里什么操作都没有,全部继承自父类(ArrayList)
    };
Copy after login

The second layer is a custom object construction block (called a non-static initialization block)

    new ArrayList<String>() {
        // 这里什么操作都没有,全部继承自父类(ArrayList)
    };
Copy after login

We get the instantiation of the ArrayList subclass through new, and then up-cast it into a reference to ArrayList

    ArrayList<String> friends = new ArrayList<String>() {{}};
Copy after login
  • The friends we get are actually references to subclasses of ArrayList, but there is no change in function

  • Compared to the regular standard way of initialization It is much simpler (but the readability of the code will be relatively poor)

Efficiency issues

Using double braces to initialize a collection may not be as efficient as the standard collection initialization step . The reason is that using double curly braces for initialization will lead to the generation of internal class files, and this process will affect the execution efficiency of the code.

First check the .class files generated by different initialization methods

For example, the following code:

public class Test1 {

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
        ArrayList<String> list1 = new ArrayList<String>() {{
            add("Harry");
            add("Tony");
            add("Tom");
            add("Jerry");
        }};

        ArrayList<String> list2 = new ArrayList<String>() {{
            add("Harry");
            add("Tony");
            add("Tom");
            add("Jerry");
        }};

        ArrayList<String> list3 = new ArrayList<String>() {{
            add("Harry");
            add("Tony");
            add("Tom");
            add("Jerry");
        }};

        ArrayList<String> list4 = new ArrayList<String>() {{
            add("Harry");
            add("Tony");
            add("Tom");
            add("Jerry");
        }};

        ArrayList<String> list5 = new ArrayList<String>() {{
            add("Harry");
            add("Tony");
            add("Tom");
            add("Jerry");
        }};
		
		……
		…snip…
		……
		
		ArrayList<String> list1000 = new ArrayList<String>() {{
            add("Harry");
            add("Tony");
            add("Tom");
            add("Jerry");
        }};
        
        System.out.println(System.currentTimeMillis());
    }
}
Copy after login

The .class list generated after compilation of Test1 is:

Test1$1.class
Test1$2.class
Test1$3.class
Test1$4.class
Test1$5.class
……
…snip…
... :

Test2.class

Only 1 .class file is generated

Running time

The first piece of code Test1 runs Result:

1508379452224

1508379452784

The running time is: 560 milliseconds

The second code Test2 running result:


1508379671505
1508379671507

The running time is: 2 milliseconds

Although this time gap will vary depending on the computer performance and operating status, it can also be explained The double brace initialization method takes longer than the conventional method


To sum up, (when the test initialization data is less (the list has not reached the auto-increment critical point)) the double brace initialization method is less efficient than the conventional method :

1. The double brace initialization method generates more .class files than the conventional method

2. The double brace initialization method takes longer to run than the conventional method

The above is the detailed content of How to use double brace initialization in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template